2011-04-20 184 views
2

我一直在爲此苦苦掙扎,儘管尋找了一段時間,卻找不到答案。我有一個自定義的數組適配器,它使用自定義對象POI填充我的列表視圖。使用自定義ArrayAdapter進行過濾

package com.WasserSportLotse; 

import java.util.ArrayList; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.Filter; 
import android.widget.ImageView; 
import android.widget.TextView; 

class poiAdapter extends ArrayAdapter<POI>{ 


    private ArrayList<POI> items; 
    private Context mContext; 
    public ArrayList<POI> allItems; 
    private Filter filter; 
    public ArrayList<POI> filtered; 

    public poiAdapter(Context context, int textViewResourceId, ArrayList<POI> items){ 
     super(context, textViewResourceId, items); 
     mContext = context; 
     this.items = items; 


} 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
      View v = convertView; 
      if (v == null) { 
       LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       v = vi.inflate(R.layout.list_items, null); 
      } 
      POI poi = items.get(position); 
      if (poi != null) { 
        TextView mDistanceTextView = (TextView) v.findViewById(R.id.listitems_distancetxt); 
        TextView mNameTextView = (TextView) v.findViewById(R.id.listitems_nametxt); 
        TextView mCategoryTextView = (TextView) v.findViewById(R.id.listitems_categorytxt); 
        if (mDistanceTextView != null) { 
         mDistanceTextView.setText(poi.getDistance()+"km");        
         } 
        if(mNameTextView != null){ 
         mNameTextView.setText(poi.getName()); 
        } 
        if(mCategoryTextView != null){ 
         mCategoryTextView.setText(poi.getType()); 
         setImage(poi.getType(), v); 

        } 
      } 
      return v; 
    } 

    private void setImage(String type, View v) { 
     if ("shopping".equals(type)){ 
      ImageView mImage = (ImageView) v.findViewById(R.id.imageView1); 
      mImage.setImageResource(R.drawable.shopping); 
     } 
     if ("liegestelle".equals(type)){ 
      ImageView mImage = (ImageView) v.findViewById(R.id.imageView1); 
      mImage.setImageResource(R.drawable.liegestelle); 
     } 
     if ("restaurant".equals(type)){ 
      ImageView mImage = (ImageView) v.findViewById(R.id.imageView1); 
      mImage.setImageResource(R.drawable.restaurant); 
     } 
     if ("schleuse".equals(type)){ 
      ImageView mImage = (ImageView) v.findViewById(R.id.imageView1); 
      mImage.setImageResource(R.drawable.schleuse); 
     } 
     if ("verein".equals(type)){ 
      ImageView mImage = (ImageView) v.findViewById(R.id.imageView1); 
      mImage.setImageResource(R.drawable.verein); 
     } 
     if ("tankstelle".equals(type)){ 
      ImageView mImage = (ImageView) v.findViewById(R.id.imageView1); 
      mImage.setImageResource(R.drawable.tankstelle); 
     } 
     if ("faekalien".equals(type)){ 
      ImageView mImage = (ImageView) v.findViewById(R.id.imageView1); 
      mImage.setImageResource(R.drawable.faekalien); 
     } 
     if ("werkstatt".equals(type)){ 
      ImageView mImage = (ImageView) v.findViewById(R.id.imageView1); 
      mImage.setImageResource(R.drawable.werkstatt); 
     } 
     if ("uebernachtung".equals(type)){ 
      ImageView mImage = (ImageView) v.findViewById(R.id.imageView1); 
      mImage.setImageResource(R.drawable.uebernachtung); 
     } 

    } 



    @Override 
    public Filter getFilter() 
    { 
     if(filter == null) 
      filter = new POITypeFilter(); 
     return filter; 
    } 

    private class POITypeFilter extends Filter 
    { 

     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 
      // NOTE: this function is *always* called from a background thread, and 
      // not the UI thread. 
      constraint = constraint.toString().toLowerCase(); 
      FilterResults result = new FilterResults(); 
      if(constraint != null && constraint.toString().length() > 0) 
      { 
       ArrayList<POI> filt = new ArrayList<POI>(); 
       ArrayList<POI> lItems = new ArrayList<POI>(); 
       synchronized (this) 
       { 
        lItems.addAll(items); 
       } 
       for(int i = 0, l = lItems.size(); i < l; i++) 
       { 
        POI m = lItems.get(i); 
        if(m.getType().toLowerCase().contains(constraint)) 
         filt.add(m); 
       } 
       result.count = filt.size(); 
       result.values = filt; 
      } 
      else 
      { 
       synchronized(this) 
       { 
        result.values = items; 
        result.count = items.size(); 
       } 
      } 
      return result; 
     } 

     @SuppressWarnings("unchecked") 
     @Override 
     protected void publishResults(CharSequence constraint, FilterResults results) { 
      // NOTE: this function is *always* called from the UI thread. 
      filtered = (ArrayList<POI>)results.values; 
      notifyDataSetChanged(); 
      clear(); 
      for(int i = 0, l = filtered.size(); i < l; i++) 
       add(filtered.get(i)); 
      notifyDataSetInvalidated(); 
     } 

    } 


} 

我想重寫getFilter方法,所以我可以通過POI.type過濾每個POI並更新listView。我打電話給getFilter像這樣

@Override 
protected void onRestart() { 
    super.onRestart(); 
    mAdapter.getFilter().filter(getFilterSettings()); 
    mAdapter.notifyDataSetChanged(); 
} 

這只是返回一個黑色的屏幕,就好像所有東西都被過濾掉了。我把斷點放在getFilter上,但是應用程序確實進入了它,所以我不確定應用程序是否使用這個代碼。有什麼明顯的我失蹤了?

更新 - 在那裏的一半

我在這裏有幾個問題。它看起來應用程序沒有調用getFilter作爲不停在那裏的斷點。實際上,Davlik虛擬機只能使用這麼多的中斷點(記住它並不是很多)。當我使用我想要分析的代碼時,它停止使用斷點。我覺得我非常不走運。

然後哪里哪里一些問題code.I改變

private ArrayList<POI> items; 

公衆。所以它可以被讀入一個新的數組,並過濾​​結果。現在只需要弄清楚如何使arrayAdapter再次過濾,但是在原始數組上。哇,這是一個漫長的過程。

回答

1

如果您想製作自定義過濾器,您只需在您的POI類上創建一個toString()方法,該方法返回您想要過濾的值。

相關問題