我正在尝试为我的应用程序创建一个带有复选框的过滤功能。我已经填充了已安装应用程序的列表,我只想过滤掉其中的几个。我的想法是获取那些被选中的复选框并将其存储到一个数组中。之后,将其显示回列表视图。
例子:
对于这个例子,我只想在点击提交按钮时列出地图和相机。有谁知道我应该怎么做才能实现这一目标?由于我对android studio的了解,我目前一无所知。
这是我的代码。
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
activity_list_app.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/app_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="3dp"
android:scaleType="centerCrop"
android:contentDescription="@null"/>
<LinearLayout
android:layout_width="240dp"
android:layout_height="wrap_content"
android:gravity="center|center_vertical"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="@+id/app_name"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_gravity="left" />
<TextView
android:id="@+id/app_package"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="left" />
</LinearLayout>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox" />
MainActivity.java
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.CheckedTextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends ListActivity{
private PackageManager packageManager = null;
private List<ApplicationInfo> applist = null;
private AppAdapter listadapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
packageManager = getPackageManager();
//ListView listview= getListView();
new LoadApplications().execute();
}
protected void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
ApplicationInfo app = applist.get(position);
try{
Intent intent = packageManager.getLaunchIntentForPackage(app.packageName);
if(intent != null){
startActivity(intent);
}
}catch(ActivityNotFoundException e){
Toast.makeText(MainActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
}catch(Exception e){
Toast.makeText(MainActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
}
}
private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list){
ArrayList<ApplicationInfo> appList = new ArrayList<ApplicationInfo>();
for(ApplicationInfo info : list){
try{
if(packageManager.getLaunchIntentForPackage(info.packageName)!=null){
appList.add(info);
}
}catch(Exception e){
e.printStackTrace();
}
}
return appList;
}
private class LoadApplications extends AsyncTask<Void, Void, Void>{
private ProgressDialog progress = null;
protected Void doInBackground(Void... params){
applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
listadapter = new AppAdapter(MainActivity.this, R.layout.activity_list_app, applist);
return null;
}
protected void onPostExecute(Void result){
setListAdapter(listadapter);
progress.dismiss();
super.onPostExecute(result);
}
protected void onPreExecute(){
progress = ProgressDialog.show(MainActivity.this, null, "Loading apps info...");
super.onPreExecute();
}
}
}
AppAdapter.java
import java.util.List;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class AppAdapter extends ArrayAdapter<ApplicationInfo>{
private List<ApplicationInfo> appList;
private Context context;
private PackageManager packageManager;
public AppAdapter(Context context, int resource, List<ApplicationInfo> objects){
super(context, resource,objects);
this.context = context;
this.appList= objects;
packageManager= context.getPackageManager();
}
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.activity_list_app,null);
}
ApplicationInfo data = appList.get(position);
if(null != data){
TextView appName = (TextView) view.findViewById(R.id.app_name);
TextView packageName = (TextView) view.findViewById(R.id.app_package);
ImageView iconView = (ImageView) view.findViewById(R.id.app_icon);
appName.setText(data.loadLabel(packageManager));
packageName.setText(data.packageName);
iconView.setImageDrawable(data.loadIcon(packageManager));
}
return view;
}
public int getCount(){
return((null != appList) ? appList.size() : 0);
}
public ApplicationInfo getItem(int position){
return((null != appList) ? appList.get(position): null);
}
public long getItemId(int position) {
return position;
}
}