2012-07-27 59 views
0

我想讓我的列表視圖與搜索框一起工作,以過濾出在列表視圖中安裝的應用程序。我已經嘗試過各種方法,比如重寫toString()方法和重寫getFilter()方法,但它們都不起作用。使用ArrayAdapter進行ListView過濾

主要活動:

public class AllApplicationsActivity extends Activity { 
    private ListView mListAppInfo; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     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 
      } 
     }); 

     // implement event when an item on list view is selected 
     mListAppInfo.setOnItemClickListener(new OnItemClickListener() { 

      public void onItemClick(AdapterView parent, View view, int pos, long id) { 
       // get the list adapter 
       AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter(); 
       // get selected item on the list 
       ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos); 
       // launch the selected application 
       //Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName); 
       Utilities.getPermissions(parent.getContext(), getPackageManager(), appInfo.packageName); 
       //Toast.makeText(MainActivity.this, "You have clicked on package: " + appInfo.packageName, Toast.LENGTH_SHORT).show(); 
      } 
     }); 


    } 
} 

AppInfoAdapter

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

經過以下鏈接[鏈接1](http://android-helper.blogspot.in/2011/03/android-search-in-listview-example.html)[鏈接2](HTTP:// seleniuminyou.blogspot.in/2011/09/android-listview-with-searchbox-sort.html)[鏈接3](http://www.androidpeople.com/android-listview-searchbox-sort-items) – Harish 2012-07-27 09:33:15

回答

1

使用TextWatcher爲你做它應該工作。您可以嘗試不調用setTextFilterEnabled,因爲這會導致列表設置它自己的過濾器,這將在列表具有焦點時起作用。

我的猜測是,ApplicationInfo.toString()將返回比您在列表中顯示哪些其他的東西。由於默認的ArrayAdapter過濾器與每個項目上的getString()匹配,因此您可能會針對意外情況進行過濾。

您可以通過使用包裝對象和壓倒一切的toString(解決這個問題),或建立自己的過濾器。

@Override 
    public Filter getFilter() { 
    return mFilter; 
    } 

    private final Filter mFilter = new Filter() { 
    @Override 
    protected FilterResults performFiltering(CharSequence charSequence) { 
     FilterResults results = new FilterResults(); 
     if (charSequence == null) { 
     return results; 
     } 

     // snip 

     results.values = /* snip */ 
     results.count = /* snip */ 
     return results; 
    } 

    @Override 
    protected void publishResults(CharSequence charSequence, FilterResults filterResults) { 
     if (filterResults != null) { 
     notifyDataSetChanged(); 
     } else { 
     notifyDataSetInvalidated(); 
     } 
    } 
    }; 

至少,提供自己的過濾器可能有助於調試。另外,我可以想象提供一個過濾器,對包名稱和標籤執行正則表達式搜索。

+0

setTextFilterEnabled沒有按甚至在註釋掉後都不會工作。我可能會再次嘗試getfilter,但我不能期待很多:( – dythe 2012-07-27 10:35:54

+0

ApplicationInfo.toString()返回什麼?如果它沒有返回包標籤,你可能會根據自己的想法進行過濾 – nas 2012-07-27 10:44:32

+0

它實際上是一個列表。編輯在主後 – dythe 2012-07-27 10:58:44