【发布时间】:2012-02-08 11:18:54
【问题描述】:
一个我无法解决的问题。
我想要达到的目标: 在来自 Web 服务调用的 AutoCompleteTextView 中显示建议。最终结果应如下所示:
到目前为止我做了什么:
在我的布局中,我有一个这样的 AutoCompleteTextView
<AutoCompleteTextView
android:id="@+id/txtAutoSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:selectAllOnFocus="true"
android:singleLine="true" />
在我的活动中,我有这个:
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());
}
}
});
AsyncTask 很简单,onPostExecute 我用webservice返回的数据设置了适配器
autoCompleteAdapter = new AdapterAutoComplete(context, ????? what should I have here?????,result);
txtAutoSearch.setAdapter(autoCompleteAdapter);
适配器如下所示:
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;
}
}
实际发生的情况:
- 网络服务返回我需要的列表,所以数据可用
- 在 Adapter 中,getView 似乎没有执行,所以我猜它做错了什么。
- 没有显示我的值的建议列表
- 我不知道适配器构造函数需要什么,用于 textViewResourceId。 我究竟做错了什么?我被卡住了,请帮忙。
【问题讨论】:
标签: android web-services autocomplete android-asynctask