【问题标题】:Dynamically Filling AutoCompleteTextView Suggestions动态填充 AutoCompleteTextView 建议
【发布时间】:2015-03-04 19:23:07
【问题描述】:

我创建了一个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);
}

正如我在 cmets 中所写,它实际上获取了数据,但我没有得到任何建议。

【问题讨论】:

标签: java android android-asynctask autocompletetextview


【解决方案1】:

我有相同的任务要实现,为此我使用了edittext下方的隐藏列表视图(我在其中输入url或搜索词),并且列表视图将根据使用textwatcher对editext的更改从api中填充。如果您需要其他解决方案,请尝试此方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 2014-05-10
    • 2013-02-10
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 2019-10-09
    相关资源
    最近更新 更多