0

I am getting data (List) from an API and I am trying to update my AutcompleteTextView with this data.

This is how I currently do :
I have a TextWatcher which calls a the method to get the data in afterTextChanged, so every time the user stops typing the method is called, and the adapter is notified with ``notifyDataSetChanged :

//in onCreate
    addressAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,suggestions_address);
    at_address.setAdapter(addressAdapter);

...

@Override
        public void afterTextChanged(Editable s) {

            autoComplete(s);

            addressAdapter.notifyDataSetChanged();

            //suggestions_address is the updated list, and when I print it I can see the 
            //results so it is not empty
            Log.i("addresses",suggestions_address.toString());

        }

...

class SuggestionQueryListener implements ResultListener<List<String>> {
    @Override
    public void onCompleted(List<String> data, ErrorCode error) {
        if (error != ErrorCode.NONE) {
            Toast.makeText(MainActivity2.this,error.toString(),Toast.LENGTH_LONG).show();
        } else {
            suggestions_address.clear();
            for(int i = 0;i<data.size();i++){
                suggestions_address.add(data.get(i));
            }
        }
    }
}
public void autoComplete(CharSequence s) {
    try {

        String term = s.toString();
        TextSuggestionRequest request = null;
        request = new TextSuggestionRequest(term).setSearchCenter(new GeoCoordinate(48.844900, 2.395658));
        request.execute(new SuggestionQueryListener());
        if (request.execute(new SuggestionQueryListener()) != ErrorCode.NONE) {
            //Handle request error
            //...
        }
    } catch (IllegalArgumentException ex) {
        //
    }
}

But it seems that the adapter is not really updated because it doesn't show the suggestions when I type something.
Also, before doing this with an AutoCompleteTextView I did it with a listView, with the same process, and everything worked well.

Any ideas or solutions would be really appreciated

EDIT : I noticed something really strange : the data is not binded to the adapter, because adapter#getCount always returns 0, even if the list is not empty. But when I remove at_address.setAdapter(addressAdapter), the data adapter is updated and adapter#getCount returns the right number of elements.

I am really confused right now, please help !

David Seroussi
  • 1,650
  • 2
  • 17
  • 34

1 Answers1

0

Instead of this:

for(int i = 0;i<data.size();i++){
            suggestions_address.add(data.get(i));
        }

you can use just this:

suggestions_address.addAll(data);

you are calling notifyDataSetChanged after you start the request, you should call it after you get the result and update the suggestions_address, so call notifyDataSetChanged inside onCompleted

SpyZip
  • 5,511
  • 4
  • 32
  • 53