Kar*_*vam 0 java android autocompletetextview android-arrayadapter textwatcher
我正在开发一个项目,在该项目中我必须访问 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);
...
}
Run Code Online (Sandbox Code Playgroud)
我已经通过打印检查了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>
Run Code Online (Sandbox Code Playgroud)
键入时不显示字符串建议的下拉列表。我也尝试过使用 textview 而不是android.R.layout.simple_dropdown_item_1line. 我在这里错过了什么?任何帮助将不胜感激