Question

I have an EditText editValue; and a Spinner spinnerActions.

I want to set the adapter of Spinner dinamically according the input that the user have inserted in the EditText

e.g.

if(editValue.getText().equals("something"){
    spinnerActions.setAdapter(adapter1);
}
else if(editValue.getText().equals("something"){
    spinnerActions.setAdapter(adapter2);
}
else{
    //show warning if the user try to select a value of the spinner
}

How could I do this?

PS The first value is the same for all adapters,if is relevant

Was it helpful?

Solution

I will try to post a more complete answer when I get a minute. First you need to implement Once you get the view, you need to set a listener to check for changes in text

EditText editText = (EditText) findViewById(R.id.edit_text_id);
editText.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        // TODO use this to set your new string value
        editValue = s;     // this won't work directly, but it is the idea of what you want to accomplish
    }
}

You can then set your adapter accordingly, for example (from above)

if(editValue.equals("something"))
    spinnerActions.setAdapter(adapter1);

OTHER TIPS

You need to have an Adapter set to spinner ,and depending on the input text ,change the data in Adapter

  final Spinner spinner = (Spinner) findViewById(R.id.spinner);
  final EditText editText = (EditText) findViewById(R.id.editText1);
  Button button = (Button) findViewById(R.id.submit);

  final List<String> stringList = new ArrayList<String>();
  final ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(ListEditActivity.this,
                android.R.layout.simple_spinner_item);

  spinner.setAdapter(spinnerAdapter);

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // clear the array list
                    stringList.clear();
                    // add the default item at first
                    stringList.add("FIRST ITEM");

                    if (editText.getText() != null && editText.getText().length() > 0) {
                        String input = editText.getText().toString();

                        if (input.toLowerCase().equalsIgnoreCase("one")) {
                            // add the spinner items for this input
                            stringList.add("ONE");
                        } else if (input.toLowerCase().equalsIgnoreCase("two")) {
                            // add the spinner items for this input
                            stringList.add("TWO");
                        } else {
                            // show dialog that invalid input
                            return;
                        }
                        // update the adapter with new data
                        spinnerAdapter.clear();
                        // adding the item will also notify the spinner to refresh the list
                        spinnerAdapter.addAll(stringList);
                    }
                }
            });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top