ListView的上市应用与复选框? [英] ListView listed apps with checkboxes?

查看:113
本文介绍了ListView的上市应用与复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView所有下载的应用程序和旁边的每个人都是两个复选框,因此您可以在任一组别或组别分类,每个应用程序。我很困惑与如何把复选框功能,我已经对这个东西读了,它仍然是一个有点麻烦给我。我的适配器是这样的:

 包com.mypackage;进口android.app.Activity;
进口android.content.pm.PackageInfo;
进口android.content.pm.PackageManager;
进口android.graphics.drawable.Drawable;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.CheckBox;
进口android.widget.TextView;进口com.example.android.home.R;进口的java.util.List;
公共类AppInfoAdapter延伸BaseAdapter {清单< PackageInfo> packageList;
活动背景;
软件包管理系统软件包管理系统;公共AppInfoAdapter(Activity上下文,列表< PackageInfo> packageList,软件包管理系统软件包管理系统){
    超();
    this.context =背景;
    this.packageList = packageList;
    this.packageManager =软件包管理系统;
}私有类ViewHolder {
    TextView的apkName;
    复选框商场,教育;
}公众诠释的getCount(){
    返回packageList.size();
}公共对象的getItem(INT位置){
    返回packageList.get(位置);
}众长getItemId(INT位置){
    返回0;
}公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    ViewHolder持有人;
    LayoutInflater吹气= context.getLayoutInflater();    如果(convertView == NULL){
        convertView = inflater.inflate(R.layout.list_layout,NULL);
        持有人=新ViewHolder();
        holder.apkName =(TextView中)convertView.findViewById(R.id.appname);
        holder.category1 =(复选框)convertView.findViewById(R.id.category1);
        holder.category2 =(复选框)convertView.findViewById(R.id.category2);
        convertView.setTag(保持器);
    }其他{
        支架=(ViewHolder)convertView.getTag();
    }
    PackageInfo packageInfo =(PackageInfo)的getItem(位置);
    可绘制APPICON =软件包管理系统
            .getApplicationIcon(packageInfo.applicationInfo);
    字符串的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);
    //为复选框更多的东西?
    返回convertView;
}}

这是基本的工作原理,列出了所有的应用程序,显示复选框很好,我只是不明白的地方我会定义,如果其中一个复选框被选中会发生什么?

主要code看起来像这样现在:

 公共类ScanApps延伸活动{
软件包管理系统软件包管理系统;
ListView的apkList;公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.scan_apps);    {软件包管理系统= getPackageManager();
    清单< PackageInfo> packageList = packageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS);
    清单< PackageInfo> installedapps =新的ArrayList< PackageInfo>();    对于(PackageInfo应用:packageList){
        如果(!isSystemPackage(应用程序)){
            installedapps.add(应用);
        }
    }
    Collections.sort(installedapps,新的比较< PackageInfo>(){
        公众诠释比较(PackageInfo O1,O2 PackageInfo){
            返回o1.applicationInfo.loadLabel(getPackageManager()).toString().compareToIgnoreCase(o2.applicationInfo.loadLabel(getPackageManager()).toString());
        }
    });
    apkList =(ListView控件)findViewById(R.id.listApps);
    apkList.setAdapter(新AppInfoAdapter(这一点,installedapps,软件包管理系统));
} //这个code加载所有已安装的Andr​​oid应用到一个列表}
私人布尔isSystemPackage(PackageInfo PKGINFO){
    返回((pkgInfo.applicationInfo.flags&放大器;!android.content.pm.ApplicationInfo.FLAG_SYSTEM)= 0)?真正
            :假的;
} //排除系统应用}


解决方案

在此块分配 holder.category1 holder.category2 来复选框实例,或者如果它存在,它包含一个持有人使用现有的convertView 对象:

 如果(convertView == NULL){
    convertView = inflater.inflate(R.layout.list_layout,NULL);
    持有人=新ViewHolder();
    holder.apkName =(TextView中)convertView.findViewById(R.id.appname);
    holder.category1 =(复选框)convertView.findViewById(R.id.category1);
    holder.category2 =(复选框)convertView.findViewById(R.id.category2);
    convertView.setTag(保持器);
}其他{
    支架=(ViewHolder)convertView.getTag();
}

正下方,你可以只设置 OnCheckedChangeListener 每个是一个新的侦听器,定义要在特定的复选框被选中做什么的。

  holder.category1.setOnCheckedChangeListener(新OnCheckedChangeListener()
{
    公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked)
    {
        如果(器isChecked)
        {
           //做一些东西
        }    }
});holder.category2.setOnCheckedChangeListener(新OnCheckedChangeListener()
{
    公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked)
    {
        如果(器isChecked)
        {
           //做其他的东西
        }    }
});

I have all downloaded apps in a ListView and next to each one are two Checkboxes so you can categorize each app in either Category1 or Category2. I'm confused with how to incorporate CheckBox functionality, I've been reading up on this stuff and it's still a little overwhelming to me. My adapter looks like this:

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

}

That basically works, lists all apps, displays checkboxes fine, I just don't understand where I'd define what happens if one of the checkboxes is checked?

Main code looks like this for now:

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

}

解决方案

In this block you assign holder.category1 and holder.category2 to CheckBox instances, or use the existing convertView if it exists, which contains a holder object:

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

Right below that, you can just set the OnCheckedChangeListener for each to be a new listener that defines whatever you want to do when that particular checkbox is checked.

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
        }

    }
});

这篇关于ListView的上市应用与复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆