我正在一個項目中,我必須從SQLite3數據庫訪問數據並在鍵入時向用戶顯示建議。我已經用AutoCompleteTextView試過了,但它不起作用。 我正在此代碼:下拉不會出現在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
。我在這裏錯過了什麼?任何幫助將不勝感激
這裏是另一個答案的鏈接: [自動完成文本視圖下拉沒有顯示(http://stackoverflow.com/a/41806707/4517450) –