views:

1195

answers:

3

Hey guys my first post here... Im 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;
    }
  }     
}
+2  A: 

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

GrkEngineer
A: 

hi alan,

Could you please help me in overriding getCount() method? Actually I also need to performFiltering and publishResults methods. It will help a lot if you could provide/publish the related code.

Kaillash
I tried hit and trial method and got little success. so when i type some characters say "ab" then the ListView gets updated having only the items which contain "ab" in their name, then i type c so new search string becomes "abc" and ListView is updated accordingly. My problem is when i press backspace to change my search string from "abc" to "ab" the ListView doesnot update. it remains the same. Please help !!
Kaillash
A: 

Hey I got the answer myself. Previously I declared my custom ArrayList say myAllItemsList as an instance variable of myActivity Class (which is extending ListActivity Class ), then I used this list in performFiltering() method. Then in publishResults() method I updated the myAllItemsList with the ArrayList returned by performFiltering() method.

Now I solved my problem using a local Arraylist say tempArrayList in myCustomArrayAdapter Class and used the myAllItemsList to filter the results in performFiltering() and updated the tempArrayList in publishResults(). I also used this tempArrayList in getView() method to update the views.

Kaillash