我想讓我的列表視圖與搜索框一起工作,以過濾出在列表視圖中安裝的應用程序。我已經嘗試過各種方法,比如重寫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;
}
}
額外
經過以下鏈接[鏈接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