2010-02-05 14 views
6

Hey guys benim ilk yazı burada ... Ben arrayadapter arraylist filtrelemek için özel bir filtre yazmaya çalışıyorum.Android ArrayAdapter'ım için filtrenin üzerine nasıl yazabilirim?

Mesela ben benim butonuna tıkladığınızda

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

Ancak, benim app beklenmedik sona benim butonuna tıkladığınızda. İşte arrayadapter ve filtre için benim kodum. Lütfen bana yardım et.

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

'LogCat'ı kullanarak bakmak ve kodunuzun içinde hata ayıklama ifadelerini bırakmak isteyebilirsiniz. 'LogCat', uygulamanın neden beklenmedik bir şekilde düştüğünü anlamak için en iyisidir, size nerede ve neden olduğunu söyleyecektir. –

+0

Anthony'nin dediği gibi, lütfen yığın izini verin. –

+0

Merhaba anthony istemi yanıt için teşekkürler .. theres benim arraylist benim için bağlı istisna dışında bir dizin gibi görünüyor ... bağdaştırıcının neden olduğu ..get ... Ben üzerinde yazmayı unutmuş bir işlevi var mı? – alan

cevap

9

ArrayAdapter sınıfındaki getCount() yöntemini geçersiz kılmanız gerekir.

+0

Bunu nasıl yapabilirim? – heyjii

+0

ArrayAdapter öğesini genişleterek, 'getCount()' yöntemini kullanan bir sınıf oluşturun. (Ya da sadece bu genel java sorusu için google.) – RaphMclee

+0

Bunun için bir yerde dokümantasyon var mı? Googling bunun gibi cevaplarla geldi, ama gerçekte nasıl olduğunu gösteren hiçbir yer bulamıyorum. Ancak dizi bağdaştırıcısında – Dave

İlgili konular