Android的自定义ArrayAdapter过滤器后无法刷新 [英] Android Custom ArrayAdapter doesn't refresh after filter

查看:153
本文介绍了Android的自定义ArrayAdapter过滤器后无法刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个自定义的 ArrayAdapter 这样我就可以使用名称/字幕认为,可从的ListView 。我有一个的EditText 接受字符串和过滤器适配器。

过滤器工作在这个意义上,它过滤了正确的对象(我可以通过点击它告诉我们,它开始用正确的额外的意图。)

然而,即使过滤功能,在适配器的项目不更新,以显示正确的信息......标题和副标题是不正确的。

假设我们通过9对的ListView ,我筛选至3项与搜索有一个项目为0,可以说经过滤项是5,6,9。 。3项显示,但其第3项原pre-搜索的ListView (0-2)的。如果我点击2项(第三项),9的内容被包含在新的意图。这是正确的搜索条件,但标题却体现出正确的信息。

我不知道我需要告诉的ListView 刷新。 我不认为它 notifyDataSetChanged();

任何帮助是AP preciated。 谢谢!

 公共类myListAdapter扩展ArrayAdapter<披萨> {
    私人的ArrayList<披萨>项目;
    私人PizzaViewHolder myListHolder;

    私有类PizzaViewHolder {
        TextView的称号;
        TextView的字幕;
    }

    公共myListAdapter(上下文的背景下,INT textViewResourceId,ArrayList的<披萨>项目){
        超(背景下,textViewResourceId,项目);
        this.items =项目;
        // TODO自动生成构造函数存根
    }

    @覆盖
    公共查看getView(INT POS,查看convertView,ViewGroup中父){
        视图V = convertView;
        如果(V == NULL){
            LayoutInflater VI =(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
            V = vi.inflate(R.layout.myList_item,NULL);
            myListHolder =新PizzaViewHolder();
            myListHolder.title =(TextView中)v.findViewById(R.id.title);
            myListHolder.subtitle =(TextView中)v.findViewById(R.id.subtitle);
            v.setTag(myListHolder);
        }其他myListHolder =(PizzaViewHolder)v.getTag();

        比萨myList上= items.get(POS);

        如果(myList上!= NULL){
            myListHolder.title.setText(myList.getTitle());
            myListHolder.subtitle.setText(myList.getSubTitle());
        }
        返回伏;
    }
}
 

这是搜索

 私人TextWatcher filterTextWatcher =新TextWatcher(){

    公共无效afterTextChanged(编辑S){
        // TODO自动生成方法存根
        如果(!s.equals()){
            。((可筛选)this.listView1.getAdapter())用getFilter()过滤器(S);
            }
    }

    公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
            之后INT){
        // TODO自动生成方法存根

    }

    公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,
            诠释计数){
        // TODO自动生成方法存根

    }

};
 

解决方案

从我所决定的,看来我需要为我的自定义ArrayAdapter自定义过滤器。 自定义ArrayAdapter有一个覆盖实施过滤器,这里是code:

 进口的java.util.ArrayList;
进口android.content.Context;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.Filter;
进口android.widget.TextView;

公共类PizzaAdapter扩展ArrayAdapter<披萨> {

私人的ArrayList<披萨>原版的;
私人的ArrayList<披萨> fitems;
私人过滤器过滤;


公共PizzaAdapter(上下文的背景下,INT textViewResourceId,ArrayList的<披萨>项目){
    超(背景下,textViewResourceId,项目);

    this.original =新的ArrayList<披萨>(项目);
    this.fitems =新的ArrayList<披萨>(项目);
    this.filter =新PizzaFilter();
}

@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
    视图V = convertView;

    如果(V == NULL){
        LayoutInflater VI =(LayoutInflater)的getContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。
        V = vi.inflate(R.layout.pizza_list_item,NULL);
    }

    比萨比萨= fitems.get(位置);

    如果(比萨!= NULL){
        字符串subtitleString =新的String([+ pizza.getPizzaType()+]+ pizza.getPizzaCategory()+:+ pizza.getPizza code());

        TextView的标题=(TextView中)v.findViewById(R.id.title);
        TextView的字幕=(TextView中)v.findViewById(R.id.subtitle);

        如果(标题!= NULL){
            title.setText(pizza.getPizzaName());
        }
        如果(字幕!= NULL){
            subtitle.setText(subtitleString);
        }
    }
    返回伏;
}

@覆盖
公共过滤用getFilter(){

    如果(过滤== NULL){
        过滤器=新PizzaFilter();
    }
    返回过滤器;
}

私有类PizzaFilter扩展过滤器{
    @覆盖
    保护FilterResults performFiltering(CharSequence的约束){
        FilterResults结果=新FilterResults();
        字符串preFIX = constraint.toString()与toLowerCase()。

        如果(preFIX == NULL || prefix.length()== 0){
            ArrayList的<披萨>名单=新的ArrayList<披萨>(原件);
            results.values​​ =清单;
            results.count =则为list.size();
        }其他{
            最后的ArrayList<披萨>名单=新的ArrayList<披萨>(原件);
            最后的ArrayList<披萨> NLIST =新的ArrayList<披萨>();
            诠释计数=则为list.size();

            的for(int i = 0; I<计数;我++){
                最终披萨比萨饼= list.get(ⅰ);
                最终的字符串值= Pizza.getPizzaName()与toLowerCase()。

                如果(value.contains(preFIX)){
                    nlist.add(比萨);
                }
                results.values​​ = NLIST;
                results.count = nlist.size();
            }
        }
        返回结果;
    }

    @燮pressWarnings(未登记)
    @覆盖
    保护无效publishResults(CharSequence的约束,FilterResults结果){
        fitems =(ArrayList的<披萨>)results.values​​;
        notifyDataSetChanged();
        明确();
        诠释计数= fitems.size();
        的for(int i = 0; I<计数;我++){
            添加(fitems.get(一));
            notifyDataSetInvalidated();
        }
    }
}
}
 

事实证明,过滤器的定制implmentation更新显示,当你搜索。希望这将帮助一些人。

So I have a custom ArrayAdapter so I can use the Title/Subtitle view that is available on the ListView. I have an EditText that accepts a string and filters the adapter.

The filter works in the sense that it is filtering the correct objects (I can tell by clicking on it and it starts the intent with the correct "extras".)

However even though the filtering works, the items in the adapter are not updated to display the correct information... The title and subtitle are incorrect.

Lets say we have items 0 through 9 on the ListView, I filter to 3 items with a search, and lets say the filtered items are 5, 6, 9... 3 items are displayed, but its the first 3 items of the original pre-search ListView (0-2). If i click on the item 2 (the third item), the contents of 9 are contained in the new intent. This is correct for the search criteria, however the title did reflect the correct information.

I'm not sure what I need to tell the ListView to refresh. I don't think its notifyDataSetChanged();

Any help is appreciated. Thanks!

    public class myListAdapter extends ArrayAdapter<Pizza>{
    private ArrayList<Pizza> items;
    private PizzaViewHolder myListHolder;

    private class PizzaViewHolder{
        TextView title;
        TextView subtitle;
    }

    public myListAdapter(Context context, int textViewResourceId, ArrayList<Pizza> items) {
        super(context, textViewResourceId, items);
        this.items = items;
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup parent){
        View v = convertView;
        if(v == null){
            LayoutInflater vi = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.myList_item, null);
            myListHolder = new PizzaViewHolder();
            myListHolder.title = (TextView)v.findViewById(R.id.title);
            myListHolder.subtitle = (TextView)v.findViewById(R.id.subtitle);
            v.setTag(myListHolder);
        }else myListHolder = (PizzaViewHolder)v.getTag();

        Pizza myList = items.get(pos);

        if (myList != null){
            myListHolder.title.setText(myList.getTitle());
            myListHolder.subtitle.setText(myList.getSubTitle());
        }       
        return v;           
    }
}

This is the search

    private TextWatcher filterTextWatcher = new TextWatcher(){

    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub
        if(!s.equals("")){  
            ((Filterable) this.listView1.getAdapter()).getFilter().filter(s);
            }
    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        // TODO Auto-generated method stub

    }

};

解决方案

From what I have determined, it seems that I needed a custom Filter for my custom ArrayAdapter. The custom ArrayAdapter has an overridden implementation of Filter, here is the code:

import java.util.ArrayList;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;

public class PizzaAdapter extends ArrayAdapter<Pizza>{

private ArrayList<Pizza> original;
private ArrayList<Pizza> fitems;
private Filter filter;


public PizzaAdapter(Context context, int textViewResourceId, ArrayList<Pizza> items) {
    super(context, textViewResourceId, items);

    this.original = new ArrayList<Pizza>(items);
    this.fitems = new ArrayList<Pizza>(items);
    this.filter = new PizzaFilter();
}

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

    if(v == null){
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.pizza_list_item, null);
    }

    Pizza pizza = fitems.get(position);

    if(pizza != null){
        String subtitleString = new String("[" + pizza.getPizzaType() + "]   " + pizza.getPizzaCategory() + ": " + pizza.getPizzaCode());

        TextView title = (TextView)v.findViewById(R.id.title);
        TextView subtitle = (TextView)v.findViewById(R.id.subtitle);

        if(title != null){
            title.setText(pizza.getPizzaName());
        }
        if(subtitle != null){
            subtitle.setText(subtitleString);
        }
    }
    return v;
}

@Override
public Filter getFilter(){

    if(filter == null){
        filter = new PizzaFilter();
    }
    return filter;
}

private class PizzaFilter extends Filter{
    @Override
    protected FilterResults performFiltering(CharSequence constraint){
        FilterResults results = new FilterResults();
        String prefix = constraint.toString().toLowerCase();

        if (prefix == null || prefix.length() == 0){
            ArrayList<Pizza> list = new ArrayList<Pizza>(original);
            results.values = list;
            results.count = list.size();
        }else{
            final ArrayList<Pizza> list = new ArrayList<Pizza>(original);
            final ArrayList<Pizza> nlist = new ArrayList<Pizza>();
            int count = list.size();

            for (int i = 0; i<count; i++){
                final Pizza pizza = list.get(i);
                final String value = Pizza.getPizzaName().toLowerCase();

                if(value.contains(prefix)){
                    nlist.add(pizza);
                }
                results.values = nlist;
                results.count = nlist.size();
            }
        }
        return results;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        fitems = (ArrayList<Pizza>)results.values;
        notifyDataSetChanged();
        clear();
        int count = fitems.size();
        for(int i = 0; i<count; i++){
            add(fitems.get(i));
            notifyDataSetInvalidated();
        }
    }
}
}

It turns out that a custom implmentation of Filter updated the display when you searched. Hopefully this will help some people.

这篇关于Android的自定义ArrayAdapter过滤器后无法刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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