Autocompletetextview自定义适配器无法正常工作

时间:2016-04-11 22:49:29

标签: android

我的主要活动中有一个autocompletetextview,我已按如下方式实施:

String[] values = new String[]{"Linux", "Ubuntu", "iPhone", "Android", "iOS"};
autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocompleteView);
autoCompleteTextView.addTextChangedListener(this);
autoCompleteTextView.setAdapter(new AutoCompleteAdapter(this, values));

我还在同一个类中实现了TextWatcher的方法。

我的adapter课程如下:

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;
}
}

以下是我的xml视图代码:

<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" />

由于我的值为Linux, Ubuntu, iPhone, Android and iOS,我希望我的自动填充工作正常,当我输入a/A时,它会显示Android。但相反,它总是在0位置显示元素,即它始终显示Linux。当我输入i/I时,它应该会给我iPhone and iOS,但它会给我Linux, Ubuntu。任何帮助表示赞赏。谢谢!

更新:当我点击下拉列表中的元素时,view中的正确值会被替换,即当我键入a/A时,linux会在下拉列表中显示Android我点击它,autocomplete显示在<a href="Empire State Building.html" onclick="initialize()">Empire State Building</a> 视图中。

1 个答案:

答案 0 :(得分:1)

您需要为Filter

实施自定义ArrayAdapter

选中tutorial