Android复选框问题

时间:2014-09-07 09:19:40

标签: android

我正在尝试开发一个显示已安装应用程序的应用程序,包括用户安装和安装的应用程序。系统应用。应用程序列表位于列表视图中,带有单独的复选框。这是我的代码:

MainActivity类:

   public class MainActivity extends Activity implements android.widget.CompoundButton.OnClickListener{

 private ProtectedAppProvider pap;
 private ListView apps;
 private PackageManager packageManager;
 private List<PackageInfo> packageList, allApps;
 private List<String> protectApps;
private CheckBox chk;


    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        apps = (ListView) findViewById(R.id.appname);
        packageManager = getPackageManager();
        packageList = packageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS); 
        allApps = packageManager.getInstalledPackages(0);

        try 
        {
            allApps.clear();
            for (int n = 0; n < packageList.size(); n++)
            {
                PackageInfo PackInfo = packageList.get(n);
                if (((packageManager.getLaunchIntentForPackage(PackInfo.packageName) != null)))
                {   
                    try
                    { 
                        allApps.add(packageList.get(n)); // add in 2nd list if it is user installed app 
                        Collections.sort(allApps,new Comparator <PackageInfo>(){
                            public int compare(PackageInfo o1,PackageInfo o2) 
                            {
                                return o1.applicationInfo.loadLabel(getPackageManager()).toString().compareToIgnoreCase(o2.applicationInfo.loadLabel(getPackageManager()).toString());
                            }
                        });
                    }catch (NullPointerException e) 
                    {
                        e.printStackTrace();
                    }
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

        apps = (ListView) findViewById(R.id.applist);
        apps.setAdapter(new ApkAdapter(this,allApps, packageManager));
   }
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub
        chk = (CheckBox) view.findViewById(R.id.checkBox1);
        apps = (ListView) findViewById(R.id.applist);
        int pos = apps.getPositionForView(view);
        PackageInfo p = allApps.get(pos);
        String str = p.applicationInfo.loadLabel(getPackageManager()).toString();

        if(((CheckBox) view).isChecked()){
            Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
        }
        }
}

ApkAdapter类:

    public class ApkAdapter extends BaseAdapter
{
    List <PackageInfo> packageList;
    Activity context;
    PackageManager packageManager;
    boolean[] itemChecked;

    public ApkAdapter(Activity context, List <PackageInfo> packageList,PackageManager packageManager) 
    {
        super();
        this.context = context;
        this.packageList = packageList;
        this.packageManager = packageManager;
        itemChecked = new boolean[packageList.size()];
    }

    private class ViewHolder 
    {
        TextView apkName;
        CheckBox ck1;
    }

    public int getCount() 
    {
        return packageList.size();
    }

    public Object getItem(int position) 
    {
        return packageList.get(position);
    }

    public long getItemId(int position) 
    {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
        final ViewHolder holder;
        LayoutInflater inflater = context.getLayoutInflater();

        if (convertView == null) 
        {
            convertView = inflater.inflate(R.layout.apklist_item, parent, false);
            holder = new ViewHolder();
            holder.apkName = (TextView) convertView.findViewById(R.id.appname);
            holder.ck1 = (CheckBox) convertView.findViewById(R.id.checkBox1);
            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, 40, 40);
          holder.apkName.setCompoundDrawables(appIcon, null, null, null);
          holder.apkName.setCompoundDrawablePadding(15);
          holder.apkName.setText(appName);
          holder.ck1.setChecked(false);

          if (itemChecked[position])
           holder.ck1.setChecked(true);
          else
           holder.ck1.setChecked(false);

          holder.ck1.setOnClickListener((MainActivity)context);

          return convertView;

    }
}

这是多选..

0 个答案:

没有答案