AggieDev
AggieDev

Reputation: 5045

Android AutoCompleteTextView showing wrong suggestions

I had the AutoCompleteTextView working perfectly until I decided to use a custom adapter, that way I could customize the look of each row. Here is what now happens:

enter image description here

As you can see, the suggestion is wrong. What is happening is as I type, it shows the correct number of suggestions, but the actual names of them are the first ones in the list. So at this point it should show only one suggestion which is Texas A&M, but it instead just shows the first one in the list. Here is how I am implementing my adapter.

//setup filter
List<String> allCollegesList = new ArrayList<String>();
for(College c : MainActivity.collegeList)
{
    allCollegesList.add(c.getName());
}
AutoCompleteDropdownAdapter adapter = new AutoCompleteDropdownAdapter(main, R.layout.list_row_dropdown, allCollegesList);
//the old way of using the adapter, which worked fine
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(main, android.R.layout.simple_dropdown_item_1line, allCollegesList);
textView.setAdapter(adapter);

As well as my actual adapter class:

public class AutoCompleteDropdownAdapter extends ArrayAdapter<String>{

    MainActivity main;
    int rowLayout;
    List<String> allCollegesList;

    public AutoCompleteDropdownAdapter(MainActivity main, int rowLayout, List<String> allCollegesList) {
        super(main, rowLayout, allCollegesList);
        this.main = main;
        this.rowLayout = rowLayout;
        this.allCollegesList = allCollegesList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        try{

            if(convertView==null){
                // inflate the layout
                LayoutInflater inflater = ((MainActivity) main).getLayoutInflater();
                convertView = inflater.inflate(rowLayout, parent, false);
            }

            // object item based on the position
            String college = allCollegesList.get(position);

            // get the TextView and then set the text (item name) and tag (item ID) values
            TextView collegeTextView = (TextView) convertView.findViewById(R.id.dropDownText);
            collegeTextView.setText(college);
            collegeTextView.setTypeface(FontManager.light);

        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;

    }
}

Upvotes: 1

Views: 1512

Answers (1)

fabian
fabian

Reputation: 82461

What happens here is that when the AutoCompleteTextView calls the getFilter method of the ArrayAdapter a Filter is returned, that takes care of filtering the ArrayAdapter, but not your allCollegesList. When you type your first characters, the methods of the Filter are called and ArrayAdapter has filtered elements at the first positions (0, 1, ...). However when the AutoCompleteTextView uses your implementation to get the Views. You use your list as if no filtering was done and use the first elements of the unfiltered list.

You can filter your own list too by overriding the getFilter method of your adapter. But that would be more coding than necessary.

You can use the ArrayAdapter's methods and not your own list, instead:

Use

String college = getItem(position);

instead of

String college = allCollegesList.get(position);

BTW:

you can get the context from parent too, using the getContext() method. That way you can decouple the Adapter from MainActivity.

Upvotes: 4

Related Questions