Ali*_*lin 6 android web-services autocomplete android-asynctask
一个我无法解决的问题.
我想要实现的目标: 在Web服务调用中显示的AutoCompleteTextView中显示建议.最终结果应如下所示:

到目前为止我做了什么:
在我的布局中,我有一个像这样的AutoCompleteTextView
<AutoCompleteTextView
android:id="@+id/txtAutoSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:selectAllOnFocus="true"
android:singleLine="true" />
Run Code Online (Sandbox Code Playgroud)
在我的活动中我有这个:
autoCompleteAdapter = new AdapterAutoComplete(context, ????? what should I have here?????, null);
autoCompleteAdapter.setNotifyOnChange(true);
txtAutoSearch.setAdapter(autoCompleteAdapter);
txtAutoSearch.addTextChangedListener(new TextWatcher() {
private boolean shouldAutoComplete = true;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
shouldAutoComplete = true;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (shouldAutoComplete) {
new GetAutoSuggestionsTask().execute(s.toString());
}
}
});
Run Code Online (Sandbox Code Playgroud)
AsyncTask非常简单,onPostExecute我使用从webservice返回的数据设置适配器
autoCompleteAdapter = new AdapterAutoComplete(context, ????? what should I have here?????,result);
txtAutoSearch.setAdapter(autoCompleteAdapter);
Run Code Online (Sandbox Code Playgroud)
适配器看起来像这样:
public class AdapterAutoComplete extends ArrayAdapter<Person> {
private Activity activity;
private List<Person> lstPersons;
private static LayoutInflater inflater = null;
public AdapterAutoComplete(Activity a, int textViewResourceId, List<Person> lst) {
super(a, textViewResourceId, lst);
activity = a;
lstPersons = lst;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return lstPersons.size();
}
public long getItemId(int position) {
return position;
}
public Person getCurrentPerson(int position) {
return lstPersons.get(position);
}
public void removeItem(int position) {
lstPersons.remove(position);
}
public static class ViewHolder {
public TextView txtName;
public TextView txtProfession;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(R.layout.people_list_item, null);
Utils.overrideFonts(activity, vi);
holder = new ViewHolder();
holder.txtName = (TextView) vi.findViewById(R.id.txtName);
holder.txtProfession = (TextView) vi.findViewById(R.id.txtProfession);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
Person o = lstPersons.get(position);
if (o.name != null) {
holder.txtName.setText(o.name);
} else {
holder.txtName.setText("N/A");
}
if (o.profession != null) {
holder.txtProfession.setText(o.profession);
} else {
holder.txtProfession.setText("N/A");
}
return vi;
}
}
Run Code Online (Sandbox Code Playgroud)
实际发生了什么:
在快速浏览文档后,您看起来就像是在正确的轨道上.
您需要异步请求才能从外部源获取结果列表.应该在每次按键时重新检查自动完成功能(取消之前的数据请求),这应该使用addTextChangedListener您正在执行的操作.到现在为止还挺好.
从这个结果列表中,您需要构建和填充适配器:您的自定义适配器应该扩展ListAdapter和Filterable(如setAdapter()文档中所述).
一旦你有一个适配器,它应该像调用一样简单:
// AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.countries_list);
textView.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
请记住,任何UI更改都应该在UI线程上或使用处理程序完成.
适配器具有以下内容时应调用getView():
.notifyDataSetChanged() 已在ListView上调用. 如果上述两个事件不会导致新的getView()s.您应该确保方法getCount()并getItem()返回正确的值.
| 归档时间: |
|
| 查看次数: |
12643 次 |
| 最近记录: |