【发布时间】:2015-01-01 11:25:45
【问题描述】:
我有一个列表视图,可以加载设备上所有已安装的应用程序。我想过滤列表视图的结果,以便用户可以通过输入其名称而不是滚动列表视图来找到应用程序。但我不知道如何实现这一点,因为我使用的是自定义适配器。感谢您提前提出任何建议。
ListView Activity 使用异步任务加载所有应用
class loadList extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
loadinglayout.setVisibility(View.VISIBLE);
lockbutton.setEnabled(false);
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
parent = getActivity().getBaseContext();
final PackageManager packageManager = parent.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resInfos = packageManager.queryIntentActivities(
intent, 0);
HashSet<String> packageNames = new HashSet<String>(0);
appInfos = new ArrayList<ApplicationInfo>(0);
for (ResolveInfo resolveInfo : resInfos) {
if ((resolveInfo.activityInfo.packageName.toString())
.equals("com.mypackage")) {
continue;
} else {
packageNames.add(resolveInfo.activityInfo.packageName);
}
}
for (String packageName : packageNames) {
try {
appInfos.add(packageManager.getApplicationInfo(packageName,
PackageManager.GET_META_DATA));
} catch (NameNotFoundException e) {
// Do Nothing
}
}
Collections.sort(appInfos,
new ApplicationInfo.DisplayNameComparator(packageManager));
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
adapter = new ApkAdapter(getActivity().getApplicationContext(),
R.layout.apklist_item, appInfos);
loadinglayout.setVisibility(View.GONE);
lView.setAdapter(adapter);
lockbutton.setEnabled(true);
}
}
我的适配器
public class ApkAdapter extends ArrayAdapter<ApplicationInfo> {
private List<ApplicationInfo> appsList = null;
private Context context;
private PackageManager packageManager;
DataBaseHandler handler;
String[] LockedApps;
Typeface fontFamily = Typeface.createFromAsset(getContext().getAssets(),
"fontawesome-webfont.ttf");
public ApkAdapter(Context context, int textViewResourceId,
List<ApplicationInfo> appsList) {
super(context, textViewResourceId, appsList);
this.context = context;
this.appsList = appsList;
packageManager = context.getPackageManager();
handler = new DataBaseHandler(getContext());
try {
handler.open();
LockedApps = handler.getPackages();
handler.close();
} catch (Exception E) {
LockedApps = null;
System.out.println("in constructor exception");
}
}
@Override
public int getCount() {
return ((null != appsList) ? appsList.size() : 0);
}
@Override
public ApplicationInfo getItem(int position) {
return ((null != appsList) ? appsList.get(position) : null);
}
@Override
public long getItemId(int position) {
return position;
}
public boolean isEnabled(int position) {
return false;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (null == view) {
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.apklist_item, null);
}
final ApplicationInfo data = appsList.get(position);
if (position % 2 == 0) {
view.setBackgroundColor(Color.parseColor("#fafafa"));
} else {
view.setBackgroundColor(Color.parseColor("#eeeeee"));
}
if (null != data) {
final TextView appName = (TextView) view
.findViewById(R.id.app_name);
TextView packageName = (TextView) view
.findViewById(R.id.app_package);
final TextView lockstatus = (TextView) view
.findViewById(R.id.lock_status);
ImageView iconview = (ImageView) view.findViewById(R.id.app_icon);
appName.setTypeface(fontFamily);
appName.setTextColor(Color.BLACK);
appName.setText(data.loadLabel(packageManager));
packageName.setText(data.packageName);
lockstatus.setTypeface(fontFamily);
try {
if (Arrays.asList(LockedApps).contains(
data.packageName.toString())) {
lockstatus.setText("\uf205");
} else if (LockedApps == null) {
lockstatus.setText("\uf204");
} else {
lockstatus.setText("\uf204");
}
} catch (Exception E) {
lockstatus.setText("\uf204");
}
lockstatus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) { // TODO Auto-generated method stub
if (lockstatus.getText().toString().equals("\uf204")) {
handler.open();
handler.insertPackage(data.packageName.toString());
LockedApps = handler.getPackages();
handler.close();
Toast.makeText(
getContext(),
appName.getText().toString()
+ " has been locked",
Toast.LENGTH_SHORT).show();
lockstatus.setText("\uf205");
} else if (lockstatus.getText().toString().equals("\uf205")) {
handler.open();
handler.deletePackage("'" + data.packageName.toString()
+ "'");
LockedApps = handler.getPackages();
handler.close();
Toast.makeText(
getContext(),
appName.getText().toString()
+ " has been unlocked",
Toast.LENGTH_SHORT).show();
lockstatus.setText("\uf204");
}
}
});
iconview.setImageDrawable(data.loadIcon(packageManager));
}
return view;
}
};
【问题讨论】:
-
你想拥有这个应用程序的功能吗? play.google.com/store/apps/…
-
@vicJordan 我只想要一个简单的列表搜索.. 例如,如果您按“w”,它应该显示所有以 W 开头或包含 W 的应用程序。
-
此应用具有相同的功能
-
我认为您正在寻找自动完成文本框。查看此链接tutorialspoint.com/android/android_auto_complete.htm
标签: android search android-listview filter