【问题标题】:ListView listed apps with checkboxes?ListView 列出了带有复选框的应用程序?
【发布时间】:2013-06-13 23:52:42
【问题描述】:

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

}

【问题讨论】:

标签: android listview checkbox


【解决方案1】:

在此块中,您将 holder.category1holder.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
        }

    }
});

【讨论】:

  • 完美,我认为它可能是这样简单的事情,只是很难辨别什么时候我必须使用其他人完成的代码。不过,我确实有一个后续问题:在 if 语句中,我插入了 holder.category1.isChecked() 并且 IDE 提示我将 holder 设为 final。这样做有什么我应该注意的吗?或者这样可以吗?
  • 没问题。是的,这是 Java 中的要求,因为您正在创建一个实现 OnCheckedChangeListener 接口的匿名类实例。为了让这个匿名类实现可以访问您的 holder 实例,您需要将其设为 final。这只是意味着您在第一次分配它之后不能将它分配给其他东西。直接声明final没问题。
猜你喜欢
  • 1970-01-01
  • 2011-09-15
  • 2012-12-11
  • 1970-01-01
  • 2020-02-11
  • 1970-01-01
  • 2015-05-04
  • 1970-01-01
  • 2017-07-09
相关资源
最近更新 更多