2012-07-26 43 views
0

我試圖根據用戶輸入的關鍵字來過濾listview的搜索欄,代碼沒有錯誤,但它根本沒有過濾。任何想法可能是什麼問題?我嘗試了各種方法,但沒有成功。從ArrayAdapter上的ListView過濾項目的問題

的OnCreate

super.onCreate(savedInstanceState); 
// set layout for the main screen 
setContentView(R.layout.layout_main); 

// load list application 
mListAppInfo = (ListView)findViewById(R.id.lvApps); 
EditText search = (EditText)findViewById(R.id.EditText01); 

mListAppInfo.setTextFilterEnabled(true); 

// create new adapter 
final AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getInstalledApplication(this), getPackageManager()); 


// set adapter to list view 
mListAppInfo.setAdapter(adapter); 


search.addTextChangedListener(new TextWatcher() { 

    public void afterTextChanged(Editable s) { 

    } 

    public void beforeTextChanged(CharSequence s, int start, int count, 
      int after) { 
    } 

    public void onTextChanged(CharSequence s, int start, int before, 
      int count) { 
     Log.e("TAG", "ontextchanged"); 
     adapter.getFilter().filter(s); //Filter from my adapter 
     adapter.notifyDataSetChanged(); //Update my view 
    } 
}); 

一個ArrayAdapter類

public class AppInfoAdapter extends ArrayAdapter<ApplicationInfo> { 

    private Context mContext; 
    PackageManager mPackManager; 

    public AppInfoAdapter(Context c, List<ApplicationInfo> list, PackageManager pm) { 
     super(c, 0, list); 
     mContext = c; 
     mPackManager = pm; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // get the selected entry 
     ApplicationInfo entry = (ApplicationInfo) getItem(position); 

     Log.e("TAG", entry.toString()); 

     // reference to convertView 
     View v = convertView; 

     // inflate new layout if null 
     if(v == null) { 
      LayoutInflater inflater = LayoutInflater.from(mContext); 
      v = inflater.inflate(R.layout.layout_appinfo, null); 
     } 

     // load controls from layout resources 
     ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon); 
     TextView tvAppName = (TextView)v.findViewById(R.id.tvName); 
     TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack); 

     // set data to display 
     ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager)); 
     tvAppName.setText(entry.loadLabel(mPackManager)); 
     tvPkgName.setText(entry.packageName); 

     // return view 
     return v; 
    } 
} 

回答

0

調用adapter.getFilter().filter(s)使用ArrayAdapter的默認String篩選邏輯,但因爲你的列表類型是ApplicationInfo,一個ArrayAdapter使用ApplicationInfo#toString()的項目比較
doesn't look like something you want to filter

+0

所以我應該在AppInfoAdapter中創建自己的過濾算法並重寫getFilter()方法? – dythe 2012-07-26 11:48:53

+0

關閉但不完全。如果你想完全控制過濾算法,你的'AppInfoAdapter'應該擴展一個'BaseAdapter'並實現'Filterable'接口(大量可用的樣本) ,但是你可能想嘗試擴展'ApplicationInfo'類和重寫它的'toString()' – eladr 2012-07-26 12:03:43

+0

那大概是什麼樣子? – dythe 2012-07-26 12:26:22

1

有解決這個

1. 使用您自己的過濾算法過濾適配器的兩種可能的方式。請參閱本博文以獲取更多信息 http://www.mokasocial.com/2010/07/arrayadapte-filtering-and-you/

2. 第二和更簡單的方法是覆蓋你可能定義

@Override 
     public String toString() { 
      return name + "\n" + description; 
     } 

自定義RowItem類的toString方法,並使用adapter.getFilter ().filter(S);因爲你正在使用它現在將工作,因爲你的適配器現在返回一個有效的字符串來過濾

+0

這種方法不適合我,我不知道爲什麼。你願意看看嗎? – Si8 2013-12-11 21:50:44

+0

當然告訴我你面臨的問題是什麼 – 2014-01-16 11:33:55

+0

我Upvoted你的答案。它的工作後,我自己的一些調整:) – Si8 2014-01-16 16:33:09