来自Web服务的Android AutoCompleteTextView,显示建议列表时遇到问题

6

我遇到了一个问题,无法解决它。

我的目标: 从 web 服务调用中显示 AutoCompleteTextView 中的建议。最终结果应如下所示:

enter image description here

我迄今为止所做的事情:

在我的布局中,我有一个类似于以下的 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中,我使用从网络服务返回的数据设置适配器。
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;
    }
}

实际发生了什么:

  • Web服务返回了我需要的列表,因此数据可用
  • 在Adapter中,getView似乎没有执行,所以我猜测有些错误已经发生在其中。
  • 没有显示与我的值相匹配的建议列表
  • 我不知道Adapter构造函数需要什么,textViewResourceId是什么。我做错了什么?我被卡住了,请帮帮我。
1个回答

7

快速浏览文档后,看起来您在正确的轨道上。

您需要异步请求从外部源获取结果列表。自动完成应该在每次按键时重新检查(取消前一个数据请求),这应该使用addTextChangedListener进行触发,这就是您正在做的事情。 到目前为止一切顺利。

从这个结果列表中,您需要构造并填充一个适配器:您的自定义适配器应扩展ListAdapter和Filterable(在setAdapter()文档中描述)。

有了适配器,只需要调用:

// AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.countries_list);
textView.setAdapter(adapter);

请记住,任何UI更改都应该在UI线程上完成或使用Handler。

当适配器有以下情况时,应调用getView():

  • 使用setAdapter()进行更改
  • 在ListView上调用了.notifyDataSetChanged()

如果以上两个事件没有导致新的getView(),则应确保方法getCount()getItem()返回正确的值。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接