【问题标题】:error with AutoCompleteTextView on choosing Item选择项目时 AutoCompleteTextView 出错
【发布时间】:2018-01-09 11:46:48
【问题描述】:

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

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

}

【问题讨论】:

    标签: android select drop-down-menu autocompletetextview


    【解决方案1】:

    //你有没有增加阈值的大小

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

    【讨论】:

    • 这是什么意思?请解释一下。我将其设置为 0
    • 我已经编辑了我的答案复制代码并测试了他的结果。
    • 不,不工作。您仅更改了阈值。这意味着输入 3 个字符后显示建议。我认为这不是一个答案。
    【解决方案2】:

    制作自定义文本视图以显示所选项目文本,看看

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

    【讨论】:

    • 你能用阵列适配器分享你的答案吗?
    猜你喜欢
    • 2013-09-12
    • 2021-06-27
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多