2010-02-05 24 views
6

Hej, mój pierwszy post tutaj ... Próbuję napisać filtr niestandardowy, aby przefiltrować listę tablic w moim moduleadapter tak, że mój listview jest filtrowany po kliknięciu przycisku.Android W jaki sposób mogę nadpisać filtr dla mojego ArrayAdapter?

Na przykład po kliknięciu na moim przycisku

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

Jednak po kliknięciu na moim przycisku, moja aplikacja zakończy niespodziewanie. Oto mój kod dla arrayadapter i filtra. Proszę pomóż mi.

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; 
    } 
    }  
} 
+1

Możesz zajrzeć do 'LogCat' i pozostawić instrukcje debugowania wewnątrz swojego kodu. 'LogCat' jest najlepszy do ustalenia, dlaczego aplikacja niespodziewanie się zawiesiła, powie ci gdzie i dlaczego. –

+0

Tak jak powiedział Anthony, proszę podać ślad stosu. –

+0

hi anthony dzięki za szybką odpowiedź .. tam wydaje się być indeksem poza granicami wyjątków dla mojej listy tablic ... spowodowanych przez adapter .get ... czy istnieje funkcja, którą zapomniałem nadpisać? – alan

Odpowiedz

9

Musisz zastąpić metodę getCount() w klasie ArrayAdapter.

+0

Jak mogę to zrobić? – heyjii

+0

Utwórz klasę, która rozszerza ArrayAdapter, w ich metodzie wpisz metodę getCount(). (Lub po prostu google dla tego ogólnego pytania na java.) – RaphMclee

+0

Czy jest gdzieś dokumentacja? Googling wymyślił takie odpowiedzi, ale nie mogę znaleźć nigdzie, gdzie tak naprawdę pokazuje. – Dave

Powiązane problemy