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

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

问题描述

我自己写了一个这样的ArrayAdapter:

i write a own ArrayAdapter like this one:

public class PoiListAdapter extends ArrayAdapter<Poi> implements Filterable {

    private Context context;
    private final List<Poi> valuesPoi;
    private ItemsFilter mFilter;

    public PoiListAdapter(Context context, List<Poi> valuesPoi) {
        super(context, R.layout.poilist);
        this.context = context;
        this.valuesPoi = valuesPoi;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.poilist, parent, false);
        TextView textViewName = (TextView) rowView.findViewById(R.id.name_poi);
        TextView textViewDis = (TextView) rowView
                .findViewById(R.id.discrip_poi);
        textViewName.setText(valuesPoi.get(position).getName());
        textViewDis.setText(valuesPoi.get(position).getDiscription());
        return rowView;
    }

    /**
     * Implementing the Filterable interface.
     */
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new ItemsFilter(this);
        }
        return mFilter;
    }

    public List<Poi> getValuesPoi() {
        return valuesPoi;
    }

    public void addValuesPoi(Poi p) {
        valuesPoi.add(p);
    }
      @Override
  public void clear() {
    valuesPoi.clear();
  }
}

对于这个适配器,我想实现一个搜索功能.因此我实现了一个自定义的过滤器类:

For this Adapter I want to implement a search function. Therefore I implement a custom Filter-Class:

public class ItemsFilter extends Filter {

private PoiListAdapter poiListAdapter;

public ItemsFilter(PoiListAdapter poiListAdapter) {
    this.poiListAdapter = poiListAdapter;
}

@Override
protected FilterResults performFiltering(CharSequence constraint) {
    constraint = constraint.toString().toLowerCase();
    FilterResults result = new FilterResults();
    ArrayList<Poi> filterList = new ArrayList<Poi>();
    if (constraint != null && constraint.toString().length() > 0) {
        ArrayList<Poi> orginalList = new ArrayList<Poi>(
                poiListAdapter.getValuesPoi());

        for (Poi p : orginalList) {
            if (p.getName().toLowerCase().contains(constraint))
                filterList.add(p);
        }
        Log.i("DEBUG", orginalList.toString());
        result.values = filterList;
        result.count = filterList.size();

    } else {

        result.values = poiListAdapter.getValuesPoi();
        result.count = poiListAdapter.getValuesPoi().size();

    }
    return result;
}

@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
    ArrayList<Poi> fitems = (ArrayList<Poi>) results.values;
    poiListAdapter.clear();
    for (Poi p : fitems) {
        poiListAdapter.addValuesPoi(p);
        poiListAdapter.notifyDataSetChanged();
    }
}

1. 问题

....是我得到了一个 java.util.concurrentmodificationexception :

....is that i got a java.util.concurrentmodificationexception for:

for (Poi p : fitems) {
            poiListAdapter.addValuesPoi(p);
            poiListAdapter.notifyDataSetChanged();
        }

我认为问题在于我想修改访问中的 Arraylist.我想我必须使用 synchronized,但我以前从未使用过它.

I think the problem is that I want to modifi a Arraylist under access. I think I have to work with synchronized, but I have never worked with it before.

更新:这个问题解决了!这里的代码:

UPDATE: This is problem is solved! Here the Code:

for(Iterator<Poi> i = fitems.iterator(); i.hasNext();) {
        Poi p = i.next();
        poiListAdapter.addValuesPoi(p);
        //poiListAdapter.notifyDataSetChanged();
    }

2. 问题

列表视图在开始时是空的.一开始我想显示所有元素!搜索元素也不会显示任何内容!列表视图目前什么都不显示!

The List view is empty at start. At the start i want to shown all elements! Also is nothing displayed by searching an element! Listview shows nothing at the moment!

推荐答案

我可以解决我的问题.这是我的解决方案!

i could solve my Problems. Here my solution!

    package hsos.ds.helper;

import hsos.ds.db.Poi;

import java.util.ArrayList;
import java.util.List;
import android.widget.Filter;

public class ItemsFilter extends Filter {

    private PoiListAdapter poiAdapter;
    private List<Poi> valuesPoi;
    private List<Poi> filteredPoi;

    public ItemsFilter(PoiListAdapter _poiAdapter) {
        this.poiAdapter = _poiAdapter;
        this.valuesPoi = poiAdapter.getValuesPoi();
        this.filteredPoi = poiAdapter.getFilteredPoi();
    }

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults result = new FilterResults();
        constraint = constraint.toString().toLowerCase();

        if (constraint == null || constraint.length() == 0) {
            ArrayList<Poi> list = new ArrayList<Poi>(valuesPoi);
            result.values = valuesPoi;
            result.count = valuesPoi.size();

        } else {
            final ArrayList<Poi> orginalList = new ArrayList<Poi>(valuesPoi);
            final ArrayList<Poi> filterList = new ArrayList<Poi>();
            int count = orginalList.size();
            for (int i = 0; i < count; i++) {
                final Poi p = orginalList.get(i);
                if (p.getName().toLowerCase().contains(constraint))
                    filterList.add(p);
            }
            result.values = filterList;
            result.count = filterList.size();
        }
        return result;
    }

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

和适配器:

公共类 PoiListAdapter 扩展 ArrayAdapter 实现可过滤{

public class PoiListAdapter extends ArrayAdapter implements Filterable {

private List<Poi> valuesPoi;
private List<Poi> filteredPoi;
private ItemsFilter mFilter;

public PoiListAdapter(Context context, List<Poi> valuesPoi) {
    super(context, R.layout.poilist);
    this.valuesPoi = new ArrayList<Poi>(valuesPoi);
    this.filteredPoi = new ArrayList<Poi>(valuesPoi);
    this.mFilter = new ItemsFilter(this);
}

@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.poilist, null);
    }

    Poi p = filteredPoi.get(position);

    if (p != null) {
        TextView tt = (TextView) v.findViewById(R.id.name_poi);
        TextView bt = (TextView) v.findViewById(R.id.discrip_poi);
        if (tt != null) {
            tt.setText(p.getName());
        }
        if (bt != null) {
            bt.setText(p.getDiscription());
        }
    }
    return v;
}

@Override
public Filter getFilter() {
    if (mFilter == null) {
        mFilter = new ItemsFilter(this);
    }
    return mFilter;
}

public List<Poi> getValuesPoi() {
    return valuesPoi;
}


public List<Poi> getFilteredPoi() {
    return filteredPoi;

}

}

要显示完整列表 onStart() 我在 onStart()-我的活动方法中插入了一个小hack",因为完整列表显示在输入:

To show the complete list onStart() i insert the a little "hack" in the onStart()-Method of my activity because the complete list is shown after an input:

if(searchText!=null){
        searchText.setText(" ");
        searchText.setText("");
    }

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

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