Reputation: 351
I have an autocompletetextview
in my main activity which i have implemented as follows:
String[] values = new String[]{"Linux", "Ubuntu", "iPhone", "Android", "iOS"};
autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocompleteView);
autoCompleteTextView.addTextChangedListener(this);
autoCompleteTextView.setAdapter(new AutoCompleteAdapter(this, values));
I have also implemented the methods for the TextWatcher
in the same class.
My adapter
class looks like this:
public class AutoCompleteAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public AutoCompleteAdapter(Context context, String[] values) {
super(context, -1, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int pos, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.autocomplete_item, parent, false);
TextView textView1 = (TextView) rowView.findViewById(R.id.autocomplete_symbol);
TextView textView2 = (TextView) rowView.findViewById(R.id.autocomplete_company_name);
textView1.setText(values[pos]);
textView2.setText(values[pos]);
return rowView;
}
}
And here is my xml
code for the view:
<AutoCompleteTextView
android:id="@+id/autocompleteView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/activity_heading_1"
android:completionThreshold="1"
android:hint="@string/autocomplete_hint" />
Since my values are Linux, Ubuntu, iPhone, Android and iOS
, i expect my autocomplete to work such that when i enter a/A
, it shows Android
. But instead it always shows the element at position 0
i.e., it always shows Linux
. When i type i/I
, it should give me iPhone and iOS
, but instead it gives me Linux, Ubuntu
. Any help is appreciated. Thanks!
UPDATE: when i click on the element from the dropdown, the correct value gets replaced in the view
i.e., when i type a/A
though linux
gets displayed in the dropdown, when i click on it, Android
gets displayed in the autocomplete
view.
Upvotes: 0
Views: 226
Reputation: 7070
You need to implement custom Filter
for your ArrayAdapter
Check this tutorial.
Upvotes: 1