列表视图中已安装应用中的复选框问题

时间:2013-09-14 07:14:45

标签: android listview checkbox

我参考了这些链接linklinklinklink

但我的问题没有得到解决。我知道有很多关于这个问题的问题,但我的问题是

我有4个人 A,B,C,D类

从A类我转到B类,其中所有已安装的应用列在listview中。在B类中,我将Packagelist发送到Class c(BaseAdapter类),然后用户选择的应用程序将传递给A类。

所以我的问题是我在哪里或如何使用POJO类将已安装的应用程序传递给baseadapter类,然后我如何获取所有选中的值(字符串应用程序名称)并将其传递给A类。 以及我如何保持复选框的检查状态。

任何帮助PLZ。

自定义适配器类

import java.util.List;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

public class AppListAdapter extends BaseAdapter {

    List<PackageInfo> packageList;
    Activity context;
    PackageManager packageManager;
    boolean[] itemChecked;
    int checkBoxCounter = 0;
    int checkBoxInitialized = 0;

    public AppListAdapter(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;
        checkBoxCounter = 0;
        checkBoxInitialized = 0;

        LayoutInflater inflater = context.getLayoutInflater();

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.apklist_item, null);
            final ViewHolder holder = new ViewHolder();

            holder.apkName = (TextView) convertView
                    .findViewById(R.id.ApkList_tvappname);
            holder.ck1 = (CheckBox) convertView
                    .findViewById(R.id.ApkList_checkBox);

            holder.ck1
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {

                            AppsSelected element = (AppsSelected) holder.ck1
                                    .getTag();
                            element.setSelected(buttonView.isChecked());

                            if (checkBoxCounter <= checkBoxInitialized) {
                                // increment counter, when we scroll the List it
                                // execute onCheckedChanged everytime so by
                                // using this stuff we can maintain the state
                                checkBoxCounter++;
                            } else {
                                element = (AppsSelected) holder.ck1.getTag();
                                element.setSelected(buttonView.isChecked());

                                if (element.isSelected())
                                    Toast.makeText(
                                            context,
                                            "You selected " + element.getName(),
                                            Toast.LENGTH_LONG).show();
                                else
                                    Toast.makeText(
                                            context,
                                            "Not selected " + element.getName(),
                                            Toast.LENGTH_LONG).show();
                            }

                        }
                    });

            convertView.setTag(holder);
            holder.ck1.setTag(packageList.get(position));

        } else {
        ((ViewHolder) convertView.getTag()).ck1.setTag(packageList.get(position));
        }
        ViewHolder 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);


        return convertView;

    }

}

1 个答案:

答案 0 :(得分:4)

你可以在这里参考我的答案,你可以做的是,在一个布尔数组中存储复选框状态然后你不会面对这个问题......

Android checkbox multiselected issue