16

My XML:

<AutoCompleteTextView
        android:id="@+id/searchAutoCompleteTextView_feed"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:completionThreshold="2"
        android:hint="@string/search" />

MY java code:

AutoCompleteTextView eT = (AutoCompleteTextView)findViewById(R.id.searchAutoCompleteTextView_feed);
eT.addTextChangedListener(this);
String[] sa = new String[]{"apple", "mango", "banana", "apple mango", "mango banana"};
ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, sa);
eT.setAdapter(aAdapter);

This is not working atall....i mean its just working like an EditTextView. Where am i wrong??

complete code:

public class FeedListViewActivity extends ListActivity implements TextWatcher{


    private AutoCompleteTextView eT;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.feed);

        eT = (AutoCompleteTextView) findViewById(R.id.searchAutoCompleteTextView_feed);
        eT.addTextChangedListener(this);

                    Thread thread = new Thread(null, loadMoreListItems);
                    thread.start();
    }

    private Runnable returnRes = new Runnable() {
        public void run() {

            //code for other purposes
        }
    };

    private Runnable loadMoreListItems = new Runnable() {
        public void run() {
            getProductNames();

            // Done! now continue on the UI thread
            runOnUiThread(returnRes);
        }
    };

    protected void getProductNames() {

            String[] sa = new String[]{"apple", "mango", "banana", "apple mango", "mango banana"};

            ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(getApplicationContext(),
                    android.R.layout.simple_dropdown_item_1line, sa);
            eT.setAdapter(aAdapter);

    }

    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub

    }
}
Housefly
  • 4,324
  • 11
  • 43
  • 70

7 Answers7

21

I just saw your other question before seeing this one. I was struggling with autocomplete for some time and I almost reverted to your new implementation of downloading all the keywords until I finally got it to work. What I did was;

//In the onCreate
//The suggestArray is just a static array with a few keywords
this.suggestAdapter = new ArrayAdapter<String>(this, this.suggestionsView, suggestArray);
//The setNotifyOnChange informs all views attached to the adapter to update themselves 
//if the adapter is changed
this.suggestAdapter.setNotifyOnChange(true);

In my textwatcher's onTextChanged method, I get the suggests using an asynctask

//suggestsThread is an AsyncTask object
suggestsThread.cancel(true);
suggestsThread = new WertAgentThread();
suggestsThread.execute(s.toString());

In the AsyncTask's onPostExecute I then update the autocompletetextview

//suggestions is the result of the http request with the suggestions
this.suggestAdapter = new ArrayAdapter<String>(this, R.layout.suggestions, suggestions);
this.suggestions.setAdapter(this.suggestAdapter);
//notifydatasetchanged forces the dropdown to be shown.
this.suggestAdapter.notifyDataSetChanged();

See setNotifyOnChange and notifyDataSetChanged for more information

Awemo
  • 875
  • 1
  • 12
  • 25
  • 22
    It seems like you have not fully understood the concept of the notifyDataSetChanged() method. You are creating a new instance of ArrayAdapter in your onPostExecute method, and set this as the adapter. The notifyDataSetChanged() method call is useless in your example. – X.X_Mass_Developer Jul 29 '14 at 11:21
3

this is a snippet from my project. I think after you got data from services all you have to do is to:

  1. clear your previous data.
  2. clear the previous adapter values.
  3. then add values to your list of data using add() or addAll() method.
  4. notify the data changed by calling notifyDataSetChanged() on adapter.

    @Override
    public void onGetPatient(List<PatientSearchModel> patientSearchModelList) {
    
    //here we got the raw data traverse it to get the filtered names data for the suggestions
    
    stringArrayListPatients.clear();
    stringArrayAdapterPatient.clear();
    for (PatientSearchModel patientSearchModel:patientSearchModelList){
    
        if (patientSearchModel.getFullName()!=null){
    
            stringArrayListPatients.add(patientSearchModel.getFullName());
    
        }
    
    }
    
    //update the array adapter for patient search
    stringArrayAdapterPatient.addAll(stringArrayListPatients);
    stringArrayAdapterPatient.notifyDataSetChanged();
    

    }

but before all this make sure you have attached the adapter to the auto complete textview if don't do it as follows:

ArrayAdapter<String> stringArrayAdapterPatient= new ArrayAdapter<String>(getActivity(),android.support.v7.appcompat.R.layout.select_dialog_item_material,stringArrayListPatients);

completeTextViewPatient.setAdapter(stringArrayAdapterPatient);
vikas kumar
  • 10,447
  • 2
  • 46
  • 52
3

Use adapter.notifyDataSetChanged() method to notify the changes in the list, If that is not working then you can show DropDown manually like autoCompleteTextView.showDropDown()

Sumit Jain
  • 1,100
  • 1
  • 14
  • 27
0
    AutoCompleteTextView eT = (AutoCompleteTextView)findViewById(R.id.searchAutoCompleteTextView_feed);
 //   eT.addTextChangedListener(this);
    String[] sa = new String[]{"apple", "mango", "banana", "apple mango", "mango banana"};
    ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, sa);
    eT.setAdapter(aAdapter);

its working just comment on et.addtext line...

jigspatel
  • 400
  • 3
  • 14
  • does version matters for autocompletetextview???...i dint know...i am some lower api...need to check by changing – Housefly May 25 '12 at 11:41
0

The only working solution after updating adapter and notifying about changes instantly show dropDown is reseting AutoCompleteTextView text again, Kotlin example:

 with(autoCompleteTextView) {
       text = text
  // Place cursor to end   
}

Java something like:

autoCompleteTextView.setText(autoCompleteTextView.getText());
// Place cursor to end  
Antonis Radz
  • 3,036
  • 1
  • 16
  • 34
0

AutoCompleteTextView.Invalidate() will do it.

0

If anyone is using a custom object array list, and facing this issue, check your model class and see if you have override the correct variable in toString. Overriede toString if you have not override yet.

public class MyModalClass {
    public int id;
    public String path;

    @Override
    public String toString() { //include this in your model and return what you need in your drop down
        return path;
    }
}
Reejesh
  • 1,745
  • 2
  • 6
  • 15