ListView 中的自定义过滤 ArrayAdapter [英] Custom Filtering ArrayAdapter in ListView

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

问题描述

我是 Android 的初学者,但我尝试进行自定义列表视图过滤,但它以某种方式起作用.我遇到的唯一问题是,我保留所有值(原始" ArrayList )的 ArrayList 在每次过滤中的项目上都越来越低.我无法解释这一点,但我认为你可以以某种方式帮助我.

I am a begginer in Android but I tried to make a custom listview filtering and I it worked somehow. The only problem I have is that the ArrayList that I kept all the values ( "original" ArrayList ) , is getting lower and lower on items in every filtering. I can't explain this but I thought that you can help me somehow .

无论如何这里是自定义 ArrayAdaptor :

Anyway here is the Custom ArrayAdaptor :

public class PkmnAdapter extends ArrayAdapter<Pkmn> {

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

public PkmnAdapter(Context context, int textViewResourceId, ArrayList<Pkmn> items) {
        super(context, textViewResourceId, items);
        this.original = items;//new ArrayList<Pkmn>();
        this.fitems = items;//new ArrayList<Pkmn>();
}

@Override
public void add(Pkmn item){
    original.add(item);
}

@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.row, null);
        }
        Pkmn pkmn = original.get(position);
        if (pkmn != null) {
                TextView tt = (TextView) v.findViewById(R.id.RlabPName);
                TextView dex = (TextView)v.findViewById(R.id.RlabDex);
                ImageView img = (ImageView)v.findViewById(R.id.RimgPkmn);

                if (tt != null) { tt.setText(pkmn.getName()); }
                if (dex != null){ dex.setText(CalcDex(pkmn.getId())); }
                if (img != null){
                    int resId = getContext().getResources().getIdentifier("dex" + pkmn.getId(), "drawable", "com.compileguy.pokebwteam");
                    img.setImageResource(resId);
                }
        }
        return v;
}

@Override
public Filter getFilter()
{
    if (filter == null)
        filter = new PkmnNameFilter();

    return filter;
}

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

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

                int count = list.size();
                final ArrayList<Pkmn> nlist = new ArrayList<Pkmn>(count);

                for (int i=0; i<count; i++)
                {
                    final Pkmn pkmn = list.get(i);
                    final String value = pkmn.getName().toLowerCase();

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

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

            if (fitems.size() > 0)
                notifyDataSetChanged();
            else
                notifyDataSetInvalidated();
        }

    }


private String CalcDex(int id){
    String s = String.valueOf(id);
    if (s.length() == 1)
        s = "00"+s;
    else if (s.length() == 2)
        s = "0"+s;
    return '#'+s;
}

}

注意:列表视图正确显示了项目,但是当我删除编辑框中的一个字母(触发过滤)时,这就是问题开始的地方.

NOTE: The listview is showing correctly the items but when for exaple I remove a letter in the editbox ( which triggers the filtering ) this is where the problems start.

--- 编辑---

@Janusz:非常感谢您的回答.这解决了我的问题.

@Janusz: Many thanks for your answer . That solved my problem .

这是对我有用的源代码,所以如果有人遇到同样的问题,他们可以试试这个:

Here is the source code that works for me , so if anyone has the same issue they could try this one :

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

public PkmnAdapter(Context context, int textViewResourceId, ArrayList<Pkmn> items) {
        super(context, textViewResourceId, items);
        this.original = new ArrayList<Pkmn>(items);
        this.fitems = new ArrayList<Pkmn>(items);
}

@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.row, null);
        }
        Pkmn pkmn = fitems.get(position);
        if (pkmn != null) {
                TextView tt = (TextView) v.findViewById(R.id.RlabPName);
                TextView dex = (TextView)v.findViewById(R.id.RlabDex);
                ImageView img = (ImageView)v.findViewById(R.id.RimgPkmn);

                if (tt != null) { tt.setText(pkmn.getName()); }
                if (dex != null){ dex.setText(CalcDex(pkmn.getId())); }
                if (img != null){
                    int resId = getContext().getResources().getIdentifier("dex" + pkmn.getId(), "drawable", "com.compileguy.pokebwteam");
                    img.setImageResource(resId);
                }
        }
        return v;
}

@Override
public Filter getFilter()
{
    if (filter == null)
        filter = new PkmnNameFilter();

    return filter;
}

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

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

                for (int i=0; i<count; i++)
                {
                    final Pkmn pkmn = list.get(i);
                    final String value = pkmn.getName().toLowerCase();

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

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            fitems = (ArrayList<Pkmn>)results.values;

            clear();
            int count = fitems.size();
            for (int i=0; i<count; i++)
            {
                Pkmn pkmn = (Pkmn)fitems.get(i);
                add(pkmn);
            }
        }

    }
}

推荐答案

你的问题是这几行:

this.original = items;
this.fitems = items;

Items 是您用于 ListView 的列表,将它放在两个不同的变量中并不会从中生成两个不同的列表.您只为列表项提供了两个不同的名称.

Items is the list you use for your ListView and putting it in two different variables does not make two different lists out of it. You are only giving the list items two different names.

您可以使用:

this.fitems = new ArrayList(items);

应该会生成一个新的列表,并且对该列表的更改只会更改 fitem 列表.

that should generate a new List and changes on this list will only change the fitems list.

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

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