0

我在 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

}
4

1 回答 1

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();
}

在其下方,您可以将OnCheckedChangeListenerfor each 设置为一个新的侦听器,该侦听器定义您在选中该特定复选框时想要执行的任何操作。

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
        }

    }
});
于 2013-06-13T15:19:23.880 回答