微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android如何覆盖ArrayAdapter的过滤器?

嘿各位我在这里的第一篇文章…我试着写一个自定义过滤器来过滤我的arrayadapter中的arraylist,这样当我点击按钮时我的listview被过滤了.

例如,当我点击我的按钮时

public void onClick(View arg0) {
            String abc = "abc";
            m_adapter.getFilter().filter(abc);
        }

但是,当我点击我的按钮时,我的应用程序意外终止.这是我的arrayadapter和过滤器的代码.请帮我.

package com.ntu.rosemobile.searchlist;

public class ResultsAdapter extends ArrayAdapter<SearchItem> implements Filterable{

public ArrayList<SearchItem> subItems;
public ArrayList<SearchItem> allItems;
private LayoutInflater inflater;
private PTypeFilter filter;

public ResultsAdapter(Context context,int textViewResourceId,ArrayList<SearchItem> items) {

    super(context,textViewResourceId,items);
        this.subItems = items;
        this.allItems = this.subItems;
        inflater= LayoutInflater.from(context);
}

@Override
public Filter getFilter() {
    if (filter == null){
      filter  = new PTypeFilter();
    }
    return filter;
  }



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

            v = inflater.inflate(R.layout.listrow,null);
        }
        SearchItem o = subItems.get(position);
        if (o != null) {
                TextView pname = (TextView) v.findViewById(R.id.productname);
                TextView neg = (TextView) v.findViewById(R.id.negNum);
                TextView pos = (TextView) v.findViewById(R.id.posNum);
                TextView neu = (TextView) v.findViewById(R.id.neuNum);

                WebImageView productPhoto = (WebImageView)v.findViewById(R.id.pPhoto);
                if(productPhoto!=null){
                    productPhoto.setimageUrl(o.getimageUrl().toString());
                    productPhoto.loadImage();
                }
                if(pname!= null){
                    pname.setText(o.getProductName().toString());
                }                    
                if (neg != null) {
                      String a =  "" + o.getNegativeReviews();
                      neg.setText(a);                            
                }
                if(neu != null){
                     String a =  "" + o.getNeutralReviews();
                     neu.setText(a);
                }
                if(pos != null){
                    String a =  "" + o.getPositiveReviews();
                    pos.setText(a);
                }
        }
        return v;
}

private class PTypeFilter extends Filter{


    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence prefix,FilterResults results) {
      // NOTE: this function is *always* called from the UI thread.
       subItems =  (ArrayList<SearchItem>)results.values;

        notifyDataSetChanged();
    }

    @SuppressWarnings("unchecked")
    protected FilterResults performFiltering(CharSequence prefix) {
          // NOTE: this function is *always* called from a background thread,and
          // not the UI thread. 

          FilterResults results = new FilterResults();
          ArrayList<SearchItem> i = new ArrayList<SearchItem>();

          if (prefix!= null && prefix.toString().length() > 0) {

              for (int index = 0; index < allItems.size(); index++) {
                  SearchItem si = allItems.get(index);
                  if(si.getPType().compareto(prefix.toString()) == 0){
                    i.add(si);  
                  }
              }
              results.values = i;
              results.count = i.size();                   
          }
          else{
              synchronized (allItems){
                  results.values = allItems;
                  results.count = allItems.size();
              }
          }

          return results;
    }
  }     
}

解决方法

您需要覆盖ArrayAdapter类中的getCount()方法.

原文地址:https://www.jb51.cc/android/308739.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐