【问题标题】:Drop down does not appear in AutoCompleteTextViewAutoCompleteTextView 中不出现下拉菜单
【发布时间】:2013-12-09 11:38:55
【问题描述】:

我正在做一个项目,我必须在其中访问 SQLite3 数据库中的数据并在输入时向用户显示建议。我已经用 AutoCompleteTextView 试过了,但它不起作用。 我正在处理这段代码:

ArrayList<String> sugestion = new ArrayList<String>();


public ViewGroup onCreateView (LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState )
{
myAutoComplete = (AutoCompleteTextView) group.findViewById(R.id.searchit);

searchDB = new searchAdapter(getActivity());

searchDB.getWritableDatabase();

myAutoComplete.addTextChangedListener(new TextWatcher(){
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            suggestion = searchDB.getSuggestion(s.toString());

            Collections.sort(suggestion);

            String[] item = new String[suggestion.size()];

            item = suggestion.toArray(item);

            for(String word : item)
                System.out.println(word);

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {                

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

searchAdapter = new ArrayAdapter<String>(getActivity() , android.R.layout.simple_dropdown_item_1line, item);

        myAutoComplete.setAdapter(searchAdapter);

...
}

我已经通过打印检查了ArrayList(来自数据库)和String[] 数组,它们排列了字符串。

我的 xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id = "@+id/group"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  >


<AutoCompleteTextView
        android:id="@+id/searchit"
         android:layout_marginTop="15dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Choose The Country" >

        <requestFocus />
    </AutoCompleteTextView>

.....
</RelativeLayout>

键入时不显示字符串建议的下拉菜单。我也尝试过使用 textview 而不是android.R.layout.simple_dropdown_item_1line 使用我自己的布局。我在这里想念什么?任何帮助将不胜感激

【问题讨论】:

标签: java android android-arrayadapter autocompletetextview textwatcher


【解决方案1】:

试试这个

myAutoComplete.showDropDown()

【讨论】:

  • 解决了。浪费了几个小时。谢谢朋友!
【解决方案2】:

试试这个……

您只在onCreate() 中设置了适配器。但是您只在TextWatcher 中的onTextChanged 内填充了item 数组的值。因此,请尝试设置 adapter afetr 填充您的 item 数组。

            myAutoComplete.addTextChangedListener(new TextWatcher(){
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            suggestion = searchDB.getSuggestion(s.toString());

            Collections.sort(suggestion);

            String[] item = new String[suggestion.size()];

            item = suggestion.toArray(item);

            for(String word : item)
                System.out.println(word);

        searchAdapter = new ArrayAdapter<String>(getActivity() , android.R.layout.simple_list_item_1, item);

        myAutoComplete.setAdapter(searchAdapter);
        myAutoComplete.setThreshold(1);

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {                

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

【讨论】:

  • @KarthikSivam 检查我的编辑.. 还没有测试过.. 但它可能会产生任何重大影响..
  • 您的答案本身是有效的。谢谢,对不起,我忘了设置阈值。
  • @KarthikSivam 很高兴它有帮助.. 认为您可能已经在 oncreate() 中设置了 threshold。无论如何,我还编辑了我的答案以供将来使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
相关资源
最近更新 更多