选择项目时AutoCompleteTextView出错

时间:2018-01-09 11:46:49

标签: android select drop-down-menu autocompletetextview

我正在使用自动完整文本视图。当我从中选择项目时,它显示第一个项目而不是显示所选项目。 这是我的代码

Utilities.setAutoCompleteTextViewAdapter(this, districtsNameList, district);

            district.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                   //selectedDistrict = (String) adapterView.getItemAtPosition(position);
                    selectedDistrict = districtsNameList.get(position);
                    Log.d("tag","============positioj===="+position)
                    Log.d("tag", "22222222222222222222222222===" + selectedDistrict);
                    district.setText(selectedDistrict);
                }
            });

它是适配器方法

 public static void setAutoCompleteTextViewAdapter(Context context, ArrayList<String> arrayList,
                                                  AutoCompleteTextView autoCompleteTextView) {
        int layoutItemId = android.R.layout.simple_dropdown_item_1line;
        ArrayAdapter<String> adapter = new ArrayAdapter<>(context, layoutItemId, arrayList);
        autoCompleteTextView.setAdapter(adapter);
        autoCompleteTextView.setThreshold(0);

}

2 个答案:

答案 0 :(得分:0)

//你增加了门槛的大小

    public static void setAutoCompleteTextViewAdapter(Context context, ArrayList<String> arrayList,
                                                  AutoCompleteTextView autoCompleteTextView) {
        int layoutItemId = android.R.layout.simple_dropdown_item_1line;
        ArrayAdapter<String> adapter = new ArrayAdapter<>(context, layoutItemId, arrayList);
        autoCompleteTextView.setAdapter(adapter);
        // here you have to change the code
        autoCompleteTextView.setThreshold(3);

}

答案 1 :(得分:0)

为显示所选项目文本制作自定义textview,看看

list_view_row.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp" >

    <TextView
        android:id="@+id/textViewItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="Item name here..."
        android:textSize="15dp" />

</RelativeLayout>

现在设置适配器如下:

myAdapter = new YourAdapter(this, R.layout.list_view_row_item, list);
            myAutoComplete.setAdapter(myAdapter);

现在实现这样的itemClick监听器:

 myAutoCompleteText.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id) {

                    RelativeLayout rl = (RelativeLayout) arg1;
                    TextView tv = (TextView) rl.getChildAt(0);
                    myAutoComplete.setText(tv.getText().toString());                         
                }     
            });