Android 如何覆盖 ArrayAdapter 的过滤器? [英] Android How do I overwrite the filter for my ArrayAdapter?

查看:27
本文介绍了Android 如何覆盖 ArrayAdapter 的过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个自定义过滤器来过滤我的 arrayadapter 中的 arraylist,这样当我点击按钮时我的列表视图就会被过滤.

I am trying to write a custom filter to filter the arraylist in my arrayadapter such that my listview is filtered when I click on the button.

例如当我点击我的按钮时

For instance when I click on my button

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

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

However, when I click on my button, my app terminate unexpectedly. Here is my code for the arrayadapter and filter. Please help me.

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() 方法.

You need to override the getCount() method in the ArrayAdapter class.

这篇关于Android 如何覆盖 ArrayAdapter 的过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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