每次AsyncTask
发生更改时,我都会创建AutoCompleteTextView
来从网址获取建议。它应该工作正常,我不知道问题是什么。
片段(使用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);
}
正如我在评论中写的,它实际上得到了数据,但我没有得到任何建议。
答案 0 :(得分:1)
我有同样的任务要实现,为此我在edittext下面使用了隐藏的列表视图(我输入了url或搜索词),listview将根据使用textwatcher在editext中的更改从api填充。如果您需要其他解决方案,请尝试此操作。