如何使用 AutoCompleteTextView 并使用来自 Web API 的数据填充它? [英] How do I Use AutoCompleteTextView and populate it with data from a web API?

查看:21
本文介绍了如何使用 AutoCompleteTextView 并使用来自 Web API 的数据填充它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的活动中使用 AutoCompleteTextView 并在用户键入时通过查询 Web API 填充数据.我该怎么做?

I want to use an AutoCompleteTextView in my activity and populate the data as the user types by querying a web API. How do I go about doing this?

我是创建一个新类并覆盖 AutoCompleteTextView.performFiltering,还是使用自定义列表适配器并提供覆盖 performFiltering 的自定义 android.widget.Filter?

Do I create a new class and override AutoCompleteTextView.performFiltering, or do I use a custom list adapter and provide a custom android.widget.Filter that overrides performFiltering?

或者有更好的方法来实现我的最终目标吗?

Or is there a better way to obtain my end goal?

我做过一些类似的事情,但它是针对快速搜索框的,它涉及实施一项服务,但我相信这不是我在这里想要做的.

I've done something somewhat similar, but it was for the Quick Search box and it involved implementing a service, but I believe that's not what I want to do here.

推荐答案

我想出了一个解决方案,我不知道它是否是最好的解决方案,但它似乎工作得很好.我所做的是创建了一个扩展 ArrayAdapter 的自定义适配器.在自定义适配器中,我覆盖了 getFilter 并创建了自己的 Filter 类来覆盖 performFiltering.这将启动一个新线程,因此它不会中断 UI.下面是一个准系统示例.

I came up with a solution, I don't know if it is the best solution, but it appears to work very well. What I did was created a custom adapter that extends ArrayAdapter. In the custom adapter I overrode getFilter and created my own Filter class that overrides performFiltering. This starts a new thread so it doesn't interrupt the UI. Below is a barebones example.

MyActivity.java

MyActivity.java

public class MyActivity extends Activity {
    private AutoCompleteTextView style;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        ...
        style = (AutoCompleteTextView) findViewById(R.id.style);
        adapter = new AutoCompleteAdapter(this, android.R.layout.simple_dropdown_item_1line); 
        style.setAdapter(adapter);
    }
}

AutoCompleteAdapter.java

AutoCompleteAdapter.java

public class AutoCompleteAdapter extends ArrayAdapter<Style> implements Filterable {
    private ArrayList<Style> mData;

    public AutoCompleteAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        mData = new ArrayList<Style>();
    }

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

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

    @Override
    public Filter getFilter() {
        Filter myFilter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                if(constraint != null) {
                    // A class that queries a web API, parses the data and returns an ArrayList<Style>
                    StyleFetcher fetcher = new StyleFetcher();
                    try {
                        mData = fetcher.retrieveResults(constraint.toString());
                    }
                    catch(Exception e) {
                        Log.e("myException", e.getMessage());
                    }
                    // Now assign the values and count to the FilterResults object
                    filterResults.values = mData;
                    filterResults.count = mData.size();
                }
                return filterResults;
            }

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

这篇关于如何使用 AutoCompleteTextView 并使用来自 Web API 的数据填充它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆