5

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;
    }
  }     
}
peterh
  • 11,875
  • 18
  • 85
  • 108
alan
  • 811
  • 2
  • 8
  • 6
  • 1
    You might want to look into using `LogCat` and leave debug statements inside of your code. `LogCat` is the best for figuring out why the application crashed unexpectedly, it will tell you where and why. – Anthony Forloney Feb 05 '10 at 15:41
  • Like Anthony said, please give the stack trace. – Mark B Feb 05 '10 at 15:55
  • hi anthony thanks for the prompt reply.. theres seems to be a index out of bound exception for my arraylist...caused by the adapter's .get ... is there a function that i have forgotten to overwrite? – alan Feb 05 '10 at 15:56
  • 1
    Hey guys thanks got it.. I need to overwrite the getCount() method of the adapter class , which i didnt.. thanks for the help.. – alan Feb 05 '10 at 16:07
  • thank you, you help me so much with your answer! – Sergey Oct 04 '10 at 10:18

1 Answers1

9

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

GrkEngineer
  • 2,122
  • 19
  • 20
  • Create a class which extends the ArrayAdapter, in their put the method `getCount()`. (Or just google for this general java question.) – RaphMclee Jul 27 '12 at 16:47
  • Is there documentation for this somewhere? Googling came up with answers like this, but I cannot find anywhere where it actually shows how. – Dave Jan 16 '13 at 05:22
  • `getCount` on the array adapter is changing the number of results returned, however. It isn't changing the content, in other words of the original list only the top _number_of_matches_ are showing. – tread Jul 09 '15 at 07:04