Android的定制AutoCompleteTextView与自定义适配器 [英] Android Custom AutoCompleteTextView with Custom Adapter

查看:681
本文介绍了Android的定制AutoCompleteTextView与自定义适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想在EditText上写东西,然后一个网页的http请求将被调用,它返回一个JSONObject其中包含一个JSON数组,其中含有内某处的值。我需要populat随从JSON对象的结果autocompletetextview的下拉列表。

我可以做的第二位,即我可以填充我需要通过扩展arrayadapter如u可以看到下面的自定义适配器类值的下拉列表。我的问题是第一位的,我怎么能覆盖AutoCompleteTextView这样,它不告诉我从数组常量过滤值,而显示我,我给它的价值?我不希望它是过滤的。这里是源$ C ​​$ C为autocompletetextview <一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.0.4_r2.1/android/widget/AutoCompleteTextView.java#91\" rel=\"nofollow\">http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.0.4_r2.1/android/widget/AutoCompleteTextView.java#91

 公共类PersonAdapter扩展ArrayAdapter
{    //我们使用构造允许提供数据对象的名单
    //要绑定。
    公共PersonAdapter(上下文的背景下,INT textViewResourceId,
            列表对象){
        超(背景下,textViewResourceId,对象);
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){        //检索在此位置绑定Person对象
        最后的人P =的getItem(位置);        //一个ViewHolder保持对孩子的参考意见,以避免不必要的
        //调用
        //在每个行findViewById()。
        ViewHolder持有人;        //当convertView不为空,我们可以直接重用,也没有
        //需要
        //为reinflate它。当convertView我们只吹一个新的View
        //提供
        //通过ListView控件为空。
        如果(convertView == NULL){
            convertView = View.inflate(的getContext(),R.layout.list_item,父母,假);            //创建两个孩子ViewHolder和存储引用
            //意见
            //我们需要将数据绑定到。
            持有人=新ViewHolder();
            holder.textName =(TextView中)convertView
                    .findViewById(R.id.textName);
            holder.textEmail =(TextView中)convertView
                    .findViewById(R.id.textEmail);
            holder.picture =(ImageView的)convertView.findViewById(R.id.image);
            holder.picture.setFocusable(假);
            holder.picture.setFocusableInTouchMode(假);
            convertView.setTag(保持器);
        }其他{
            //获取ViewHolder回才能到TextView的快速访问
            //和ImageView的。
            支架=(ViewHolder)convertView.getTag();
        }        //与支架有效地结合的数据。
        holder.textName.setText(p.getName());
        holder.textEmail.setText(p.getEmail());
        holder.picture.setImageResource(p.getResImage());
                //点​​击图片
        holder.picture.setOnClickListener(新OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                Toast.makeText(的getContext(),
                        点击了+ p.getName()+的图片,
                        Toast.LENGTH_SHORT).show();            }
        });        返回convertView;
    }    / **
     *
     *内支架类在ListView单行视图
     *
     * /
    静态类ViewHolder {
        TextView的textName,textEmail;
        ImageView的图片;
    }}


解决方案

在您的PersonAdapter覆盖用getFilter()方法返回你的,你必须要实现的两个方法的定制内部过滤器:performFiltering()和publishResults(),performFiltering运行在工作​​线程,在这里你打电话给你的Web请求,在publishResults只需调用清除(),并添加()的项目

Basically, I wish to write something in the edittext, then a web http request will be called that returns a JSONObject which contains a JSON array, which contain the values somewhere inside it. I need to populat the dropdown list that comes with autocompletetextview with the results from the JSON Object.

I can do the second bit, i.e. I can populate the dropdown list with the values I need by using a custom adapter class that extends arrayadapter as u can see below. My problem is with the first bit, how can I override AutoCompleteTextView such that it doesn't show me filtered constant values from an array, rather shows me the values that I give it ? I don't want it to be filterable at all. Here is the sourcecode for autocompletetextview http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.4_r2.1/android/widget/AutoCompleteTextView.java#91

public class PersonAdapter extends ArrayAdapter
{

    // we use the constructor allowing to provide a List of objects for the data
    // to be binded.
    public PersonAdapter(Context context, int textViewResourceId,
            List objects) {
        super(context, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // retrieve the Person object binded at this position
        final Person p = getItem(position);

        // A ViewHolder keeps references to children views to avoid unneccessary
        // calls
        // to findViewById() on each row.
        ViewHolder holder;

        // When convertView is not null, we can reuse it directly, there is no
        // need
        // to reinflate it. We only inflate a new View when the convertView
        // supplied
        // by ListView is null.
        if (convertView == null) {
            convertView = View.inflate(getContext(), R.layout.list_item, parent, false);

            // Creates a ViewHolder and store references to the two children
            // views
            // we want to bind data to.
            holder = new ViewHolder();
            holder.textName = (TextView) convertView
                    .findViewById(R.id.textName);
            holder.textEmail = (TextView) convertView
                    .findViewById(R.id.textEmail);
            holder.picture = (ImageView) convertView.findViewById(R.id.image);
            holder.picture.setFocusable(false);
            holder.picture.setFocusableInTouchMode(false);
            convertView.setTag(holder);
        } else {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();
        }

        // Bind the data efficiently with the holder.
        holder.textName.setText(p.getName());
        holder.textEmail.setText(p.getEmail());
        holder.picture.setImageResource(p.getResImage());
                //click on the picture
        holder.picture.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(),
                        "Clicked on " + p.getName() + "'s picture",
                        Toast.LENGTH_SHORT).show();

            }
        });

        return convertView;
    }

    /**
     *
     * Inner holder class for a single row view in the ListView
     *
     */
    static class ViewHolder {
        TextView textName, textEmail;
        ImageView picture;
    }

}

解决方案

in your PersonAdapter override getFilter() method returning your custom inner Filter where you have to implement its two methods: performFiltering() and publishResults(), performFiltering is run in the worker thread and here you call your web request, in publishResults just call clear() and add() the items

这篇关于Android的定制AutoCompleteTextView与自定义适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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