在过滤listview后清除数据后,我没有获得orignal listview android?

时间:2013-12-31 10:47:06

标签: android listview

我在适配器类中实现了一个过滤方法,如下所示。

public class EntryAdapter extends ArrayAdapter<Item> implements Filterable {

    private Context context;
    private ArrayList<Item> items;
    private ArrayList<Item> fitems;
    private LayoutInflater vi;
    private contact contact;
     private ItemsFilter mFilter;
    public EntryAdapter(Context context,ArrayList<Item> items) {
        super(context,0, items);
        this.context = context;
        this.contact=(contact) context;
        this.items = items;
        vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

@Override
public int getCount() {
    // TODO Auto-generated method stub

    return items.size();
}

@Override
public Item getItem(int position) {
    // TODO Auto-generated method stub
    return super.getItem(position);
}

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

        final Item i = items.get(position);

        if (i != null) {
            if(i.isSection()){
                SectionItem si = (SectionItem)i;
                v = vi.inflate(R.layout.list_item_section, null);

                v.setOnClickListener(null);
                v.setOnLongClickListener(null);
                v.setLongClickable(false);

                final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
                sectionView.setText(si.getTitle());

            }else{
                EntryItem ei = (EntryItem)i;
                v = vi.inflate(R.layout.entrylist, null);
                final TextView title = (TextView)v.findViewById(R.id.list_item_entry_title);
                final TextView subtitle = (TextView)v.findViewById(R.id.list_item_entry_summary);


                if (title != null) 
                    title.setText(ei.title);
                if(subtitle != null)
                    subtitle.setText(ei.subtitle);
            }
        }
        return v;
    }

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

        }

     private class ItemsFilter extends Filter{

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();

            if (constraint == null || constraint.length() == 0){
                results.values = items;
                results.count = items.size();

            }
            else{

                 ArrayList<Item> itemsList = new ArrayList<Item>();

                 for (Item i : items){

                     if (i.toString().toUpperCase().startsWith(constraint.toString().toUpperCase()))
                            itemsList.add(i);
                 }
                 results.values = itemsList;
                    results.count = itemsList.size();
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
             if (results.count == 0){

                 notifyDataSetInvalidated();
             }
             else{

                 ArrayList<Item> lst = (ArrayList<Item>)results.values;
                 ArrayList<Item> itemsList = new ArrayList<Item>(lst);
                 //this.items=mItems;
                items =itemsList;              
                notifyDataSetChanged();
             }

        }


     }

}

这是我在活动中实施过滤的地方。

search.addTextChangedListener(new TextWatcher() {

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        //s= items.toString();
                        contact.this.adapter.getFilter().filter(s.toString());
                        adapter.notifyDataSetChanged();
                    }

过滤工作正常,但是当我从键盘上按下十字按钮时,我没有收到orignal listview。我只得到我过滤的数据。我想如果用户再次将editext设为空白,则应加载orignal listview。

1 个答案:

答案 0 :(得分:0)

您正在将过滤后的数据分配到原始列表,这就是您无法获得原始列表的原因。 enter image description here

您应该将过滤结果列表分配给您的fitems。 例如

   fitems = (ArrayList<Item>) results.values;
            notifyDataSetChanged();

好吧,试试这个精​​致的代码: -

public class EntryAdapter extends ArrayAdapter<Item> implements Filterable {

private Context context;
private ArrayList<Item> items;
private ArrayList<Item> fitems;
private LayoutInflater vi;
private contact contact;
 private ItemsFilter mFilter;
public EntryAdapter(Context context,ArrayList<Item> items) {
    super(context,0, items);
    this.context = context;
    this.contact=(contact) context;
    this.items = items;
    this.fitems = new ArrayList<Item>(); 
    vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
// TODO Auto-generated method stub

return items.size();
}

@Override
public Item getItem(int position) {
// TODO Auto-generated method stub
return super.getItem(position);
}

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

    final Item i = fitems.get(position);

    if (i != null) {
        if(i.isSection()){
            SectionItem si = (SectionItem)i;
            v = vi.inflate(R.layout.list_item_section, null);

            v.setOnClickListener(null);
            v.setOnLongClickListener(null);
            v.setLongClickable(false);

            final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
            sectionView.setText(si.getTitle());

        }else{
            EntryItem ei = (EntryItem)i;
            v = vi.inflate(R.layout.entrylist, null);
            final TextView title = (TextView)v.findViewById(R.id.list_item_entry_title);
            final TextView subtitle = (TextView)v.findViewById(R.id.list_item_entry_summary);


            if (title != null) 
                title.setText(ei.title);
            if(subtitle != null)
                subtitle.setText(ei.subtitle);
        }
    }
    return v;
}

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

    }

  private class ItemsFilter extends Filter{

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();
       ArrayList<Item> FilteredArrList = new ArrayList<Item>();
        if (constraint == null || constraint.length() == 0){
            results.values = items;
            results.count = items.size();

        }
        else{
             constraint = constraint.toString().toUpperCase();
             ArrayList<Item> itemsList = new ArrayList<Item>();

             for (Item i : items){

                 if (i.toString().toUpperCase().startsWith(constraint))
                        FilteredArrList.add(i);
             }
             results.values = FilteredArrList;
                results.count = FilteredArrList.size();
        }

        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint,
            FilterResults results) {
     fitems = (ArrayList<Item>) results.values;
                notifyDataSetChanged();
    }
 }

 }