paland3
paland3

Reputation: 61

Autocompleteview with custom list item is not working as expected

I would like to use the android autocomplete text view with custom dropdown list items (not only the string will be included). It works in a special way: it finds the corresponding items in my arraylist, and if i click on an item, the correct string will appear in the text box. However, the dropdown menu does not show the the correct strings, but the first X entries (where x is the num_of_results) of my arraylist. Example: arraylist: a, b, c, aa, ab, ac entered text: a my resaults: a, b, c, aa (notice the number of resaults is correct) and if i click on b, the textview gets aa (the second result)

As i guess, i have problems with my adapter, or my customAutoComplete class. Here is my CustomAutoCompleteView class.

public class CustomAutoCompleteView extends AutoCompleteTextView {


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

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

public CustomAutoCompleteView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public boolean enoughToFilter() { 
    return true;
}



protected void onFocusChanged (boolean focused, int direction, Rect previouslyFocusedRect) 
{
    if(focused)
        performFiltering("", 0);
    super.onFocusChanged(focused, direction, previouslyFocusedRect);
}

And this is my listadapter:

public class ListAdapter extends ArrayAdapter<StopData> {

public ListAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
    // TODO Auto-generated constructor stub
}

private List<StopData> stops;

public ListAdapter(Context context, int resource, List<StopData> stops) {

    super(context, resource, stops);

    this.stops = stops;

}

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

    View v = convertView;

    if (v == null) {

        LayoutInflater vi;
        vi = LayoutInflater.from(getContext());
        v = vi.inflate(R.layout.megalloelem, null);

    }

    StopData p = stops.get(position);

    if (p != null) {

        TextView stopname = (TextView) v.findViewById(R.id.megallo);

        if (stopname != null) {
            stopname.setText(p.name);
        }
    }

    return v;

}

The getView function already gets the "wrong" indexes, so the problem is not (only) there. Any ideas how to get the indexes of the results from the original arraylist? Sincerely, paland3

Upvotes: 1

Views: 263

Answers (1)

hunyadym
hunyadym

Reputation: 2243

Use

StopData p = getItem(position);

instead of

StopData p = stops.get(position);

This is because the adapter handles the filtering, and it will give back the correct item.

Upvotes: 1

Related Questions