我已经在ListView中下载了所有应用程序,并且每个应用程序旁边都有两个复选框,因此您可以对Category1或Category2中的每个应用程序进行分类。我对如何结合CheckBox功能感到困惑,我一直在阅读这些东西,它对我来说仍然有点压倒性的。我的适配器看起来像这样:
package com.mypackage;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import com.example.android.home.R;
import java.util.List;
public class AppInfoAdapter extends BaseAdapter {
List<PackageInfo> packageList;
Activity context;
PackageManager packageManager;
public AppInfoAdapter(Activity context, List<PackageInfo> packageList, PackageManager packageManager) {
super();
this.context = context;
this.packageList = packageList;
this.packageManager = packageManager;
}
private class ViewHolder {
TextView apkName;
CheckBox arcade, educational;
}
public int getCount() {
return packageList.size();
}
public Object getItem(int position) {
return packageList.get(position);
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_layout, null);
holder = new ViewHolder();
holder.apkName = (TextView) convertView.findViewById(R.id.appname);
holder.category1 = (CheckBox) convertView.findViewById(R.id.category1);
holder.category2 = (CheckBox) convertView.findViewById(R.id.category2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
PackageInfo packageInfo = (PackageInfo) getItem(position);
Drawable appIcon = packageManager
.getApplicationIcon(packageInfo.applicationInfo);
String appName = packageManager.getApplicationLabel(
packageInfo.applicationInfo).toString();
appIcon.setBounds(0, 0, 55, 55);
holder.apkName.setCompoundDrawables(appIcon, null, null, null);
holder.apkName.setCompoundDrawablePadding(15);
holder.apkName.setText(appName);
//more stuff for checkboxes?
return convertView;
}
}
这基本上有用,列出所有应用程序,显示复选框很好,我只是不明白我在哪里定义如果检查其中一个复选框会发生什么?
主要代码现在看起来像这样:
public class ScanApps extends Activity {
PackageManager packageManager;
ListView apkList;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scan_apps);
{packageManager = getPackageManager();
List<PackageInfo> packageList = packageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS);
List<PackageInfo> installedapps = new ArrayList<PackageInfo>();
for(PackageInfo apps: packageList){
if(!isSystemPackage(apps)){
installedapps.add(apps);
}
}
Collections.sort(installedapps, new Comparator<PackageInfo>() {
public int compare(PackageInfo o1, PackageInfo o2) {
return o1.applicationInfo.loadLabel(getPackageManager()).toString().compareToIgnoreCase(o2.applicationInfo.loadLabel(getPackageManager()).toString());
}
});
apkList = (ListView) findViewById(R.id.listApps);
apkList.setAdapter(new AppInfoAdapter(this, installedapps, packageManager));
} //this code loads all installed Android Apps into a list
}
private boolean isSystemPackage(PackageInfo pkgInfo) {
return ((pkgInfo.applicationInfo.flags & android.content.pm.ApplicationInfo.FLAG_SYSTEM) != 0) ? true
: false;
} //excludes system apps
}
答案 0 :(得分:0)
在此块中,您将holder.category1
和holder.category2
分配给CheckBox实例,或使用现有的convertView(如果存在),其中包含holder
对象:
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_layout, null);
holder = new ViewHolder();
holder.apkName = (TextView) convertView.findViewById(R.id.appname);
holder.category1 = (CheckBox) convertView.findViewById(R.id.category1);
holder.category2 = (CheckBox) convertView.findViewById(R.id.category2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
在下面,您可以将每个OnCheckedChangeListener
设置为新的侦听器,定义在选中该特定复选框时您想要执行的操作。
holder.category1.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// Do some stuff
}
}
});
holder.category2.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// Do other stuff
}
}
});