Abhinav Pawar
Abhinav Pawar

Reputation: 431

Update suggestions on autocomplete textview item click

I have implemented a Autocomplete textview.I have configured it in such a way that every time i click autocomplete textview i show up the suggestions.Everything is working fine except that the suggestions are not updated after I have selected something from the list.Here is my code:

    arrayAdapter = new ArrayAdapter<>(
            HomeActivity.this, android.R.layout.simple_dropdown_item_1line, array);

    textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTV);
    textView.setAdapter(arrayAdapter);
    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View arg0) {

            textView.showDropDown();

        }
    });
    textView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
          arrayAdapter.notifyDataSetChanged();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

For instance if i have String array like {"a", "abvc", "ajedghed", "b", "bdvhd", "bwgdydg", "c", "cswjwwd"} And i type b,it shows all relevant suggestions with "b" like "b","bdvhd", "bwgdydg".This works fine but if i select any of the suggestion and again click autocomplete textview it still shows previous result drop down on all b's.

I tried adding notifyDataSetChanged() on item click but no luck.

Upvotes: 2

Views: 1890

Answers (2)

NezSpencer
NezSpencer

Reputation: 680

I understand that when you click a text from the drop down, you want the autoComplete suggestion to change based on the new text you selected. To achieve that, you need to extend the autoCompleteTextView class, then override replaceText() then in your onItemSelected, call textView.replaceText((String)adapterView.getItemAtPosition(i));

something like this:

Step1: override AutoCompleteTextView

public class AutoComplete extends AutoCompleteTextView {

public AutoComplete(Context context) {
    super(context);
}

public AutoComplete(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public AutoComplete(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

public AutoComplete(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
protected void replaceText(CharSequence text) {
    super.replaceText(text);
}

}

Step2: change the autoCompleteTextView inyour xml to use the one to created

 <com.example.tester.AutoComplete
        android:id="@+id/autocomplete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:completionThreshold="1"

/>

Step3 add the replaceText in your code like this:

final String [] array={"a", "abvc", "ajedghed", "b", "bdvhd", "bwgdydg", "c", "cswjwwd",
            "ssehdk","chakra"};
    final AutoComplete textView=(AutoComplete) findViewById(R.id.autocomplete);
    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            textView.showDropDown();
        }
    });
    if (textView!=null)
    textView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,array));

    textView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

            textView.replaceText((String)adapterView.getItemAtPosition(i));
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

Upvotes: 1

Paras Jain
Paras Jain

Reputation: 49

Try Doing something like this when the user clicks again , so that the textview does not contain any text and the adapter returns all the items in the list.

    if(textView.getText().toString() != null && textView.getText().toString().trim() != "")  {
        textView.setText(null);
    }

Upvotes: 1

Related Questions