[英]Dynamically Filling AutoCompleteTextView Suggestions
我創建了一個AsyncTask
以在每次AutoCompleteTextView
發生更改時從URL提取建議。 它應該可以正常工作,我不知道問題出在哪里。
片段(帶有AutoCompleteTextView):
atv = (AutoCompleteTextView) view.findViewById(R.id.atvMovieName);
// adding event listeners for text on the auto complete text view
atv.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
SuggestionFetcher fetcher = new SuggestionFetcher(getActivity(), atv);
String title = s.toString().replace(" ", "%20");
try {
URL url = new URL("http://imdbapi.com/?s=" + title + "&r=xml");
fetcher.execute(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
SuggestionFetcher:
// suggestion titles will be saved here
private Stack<String> suggestions;
// this is the auto complete text view we will be handling
private AutoCompleteTextView atv;
// and it's adapter
private ArrayAdapter<String> adapter;
// context of the activity or fragment
private Context context;
private static final String TAG = "Suggestion Fetcher";
public SuggestionFetcher(Context c, AutoCompleteTextView atv) {
this.atv = atv;
this.context = c;
this.suggestions = new Stack<String>();
}
@Override
protected Stack<String> doInBackground(URL... params) {
// get the data...
this.suggestions.add(title);
return this.suggestions;
}
@Override
protected void onPostExecute(Stack<String> strings) {
super.onPostExecute(strings);
Log.v(TAG, "finished with the data: " + strings); // works, shows the results I wanted
// update the list view
this.adapter = new ArrayAdapter<String>(this.context, android.R.layout.simple_list_item_1, strings);
this.atv.setAdapter(this.adapter);
}
正如我在評論中所寫,它實際上是在獲取數據,但沒有任何建議。
我要執行相同的任務,為此我在edittext(在其中輸入url或搜索詞的地方)下面使用了一個隱藏的listview,並且將根據textexter中editext的更改從api中填充listview。您需要另一種解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.