android - 更新有关自动完成 TextView 项目单击的建议

标签 android android-arrayadapter autocompletetextview

我已经实现了自动完成 TextView 。我以这样的方式配置它,每次单击自动完成 TextView 时,我都会显示建议。除了我从列表中选择某些内容后建议不会更新之外,一切都工作正常.这是我的代码:

    arrayAdapter = new ArrayAdapter<>(
            HomeActivity.this, android.R.layout.simple_dropdown_item_1line, array);

    textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTV);
    textView.setAdapter(arrayAdapter);
    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View arg0) {

            textView.showDropDown();

        }
    });
    textView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
          arrayAdapter.notifyDataSetChanged();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

例如,如果我有像 {"a", "abvc", "ajedghed", "b", "bdvhd", "bwgdydg", "c", "cswjwwd"} 这样的字符串数组 我输入 b,它显示所有与“b”相关的建议,如“b”、“bdvhd”、“bwgdydg”。这工作正常,但如果我选择任何建议并再次单击自动完成 TextView ,它仍然显示以前的结果下拉列表在所有 b 上。

我尝试在项目点击上添加notifyDataSetChanged(),但没有成功。

最佳答案

据我所知,当您单击下拉列表中的文本时,您希望自动完成建议根据您选择的新文本进行更改。为此,您需要扩展 autoCompleteTextView 类,然后重写 replaceText(),然后在 onItemSelected 中调用 textView.replaceText((String)adapterView.getItemAtPosition(i));

像这样:

第 1 步:覆盖 AutoCompleteTextView

public class AutoComplete extends AutoCompleteTextView {

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

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

public AutoComplete(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

public AutoComplete(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
protected void replaceText(CharSequence text) {
    super.replaceText(text);
}

}

第 2 步:更改 xml 中的 autoCompleteTextView 以使用要创建的内容

 <com.example.tester.AutoComplete
        android:id="@+id/autocomplete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:completionThreshold="1"

/>

第三步在代码中添加替换文本,如下所示:

final String [] array={"a", "abvc", "ajedghed", "b", "bdvhd", "bwgdydg", "c", "cswjwwd",
            "ssehdk","chakra"};
    final AutoComplete textView=(AutoComplete) findViewById(R.id.autocomplete);
    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            textView.showDropDown();
        }
    });
    if (textView!=null)
    textView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,array));

    textView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

            textView.replaceText((String)adapterView.getItemAtPosition(i));
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

关于android - 更新有关自动完成 TextView 项目单击的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37293176/

相关文章:

Android ProgressDialog运行时异常错误

java - ListView的Android自定义行项目

android - AutoCompleteTextView 适配器返回旧值

android - 在 AutocompleteTextView 中显示所有项目而不写入文本

java - 将 pdf 文档从 asset 导入到 PdfDocument 对象中

android - 为什么有些安卓手机没有安装 Google Play 服务?

Android - 一个应用程序中有两个或多个 GCM token

java - ArrayAdapter 中带有按钮的自定义 View

java - ArrayAdapter 可能导致应用程序崩溃

android - AutoCompleteTextView、DropDown、setOnDismissListener、<17 api