gpt4 book ai didi

android - ArrayAdapter 较晚从 AutoCompleteTextAdapter 中的 Webservice 更新

转载 作者:行者123 更新时间:2023-11-29 22:23:35 25 4
gpt4 key购买 nike

我有一个使用 ArrayAdapter 的 AutocompleteTextView。当文本更改时,适配器从网络服务更新。此更新是在 AsyncTask 中完成的,这应该是一个很好的做法。这或多或少是有效的,因为每个按键后的建议都是基于在前一个按键中检索到的字符串。这个页面有几个相关的问题,但没有一个答案对我有用。无论如何,我有一个解决方案,但效率低下,我不知道为什么“官方解决方案”会失败。

我认为关键在于在后台更新 ArrayAdapter 的函数。这就是我在对 Web 服务的异步调用中所做的:

private class DoAutoCompleteSearch extends AsyncTask<String, Void, Map<String, String>> {

@Override
protected Map<String, String> doInBackground(String... params) {

// Ask the webservice for data
Map<String, String> autoComplete = GetResource.dataList(params[0]);
return autoComplete;
}

@Override
protected void onPostExecute(Map<String, String> result) {

//mAutoCompleteAdapter.clear(); * This should work but does not *

/* If it is set a new adapter in the AutoCompleteTextView, the whole thing works properly */

mAutoCompleteAdapter = new ArrayAdapter<String>(mAutoCompleteAdapter.getContext(), android.R.layout.simple_dropdown_item_1line);
mACTV.setAdapter(mAutoCompleteAdapter);

for (Map.Entry<String, String> entry : result.entrySet()) {
mAutoCompleteAdapter.add(entry.getKey());
}
}
}

我试过使用 mAutoCompleteAdapter.clear() 并在各处设置 mAutoCompleteAdapter.notifyDataSetChanged(),但没用。

最佳答案

我也尝试了很多让这种方法奏效,但没有比你更成功。但是我找到了另一种方法来完成你想要的;扩展 ArrayAdapter 并实现 Filterable。当工作线程中的 AutoCompleteTextView 调用时,此类将从数据库中进行实际提取:

public class MyAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {

private List<String> mData = new ArrayList<String>();
private Server mServer;

public MyAutoCompleteAdapter(Server server, Context context, int textViewResourceId) {
super(context, textViewResourceId);
mServer = server;
}

@Override
public int getCount() {
return mData.size();
}

@Override
public String getItem(int index) {
return mData.get(index);
}

@Override
public Filter getFilter() {
Filter myFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
// This method is called in a worker thread

FilterResults filterResults = new FilterResults();
if(constraint != null) {
try {
// Here is the method (synchronous) that fetches the data
// from the server
List<String> results = mServer.searchForItems(constraint.toString());
filterResults.values = results;
filterResults.count = results.size();
}
catch(Exception e) {}

}
return filterResults;
}

@Override
protected void publishResults(CharSequence contraint, FilterResults results) {
if(results != null && results.count > 0) {
mData = (List<String>)results.values;
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}
};
return myFilter;
}
}

编辑:我改进了上面的代码,因为我有时会遇到异常 java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification。为了解决这个问题,我将 mData 的更新移到了 publishResults() 中。

关于android - ArrayAdapter 较晚从 AutoCompleteTextAdapter 中的 Webservice 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6546376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com