自定义列表适配器 - 选择所有复选框按钮

时间:2014-04-09 14:51:36

标签: android listview checkbox

我设法实现了一个带有复选框的自定义Listview。用户可以点击列表视图中的一行,然后选中该复选框。即使用户滚动,也会存储这些位置。

listview绑定到Async任务的onPostExecute中的适配器。

 protected void onPostExecute(Boolean result) {
           ImageAdapter adapter = new ImageAdapter(this, pix, paths);
           lstView.setAdapter(adapter);
        }

我可以点击列表项并选中该项的复选框,如下所示:

lstView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long row) {

CheckBox cb = (CheckBox) view.findViewById(R.id.checkBox1);
                        cb.performClick();
}

我想介绍一个按钮,它会检查所有复选框。

我试过了,但它没有用。

mSelectAllButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox cb = (CheckBox) v.findViewById(R.id.checkBox1);//find the checkbox in the custom list

                    int cntChoice = mListView.getCount();
                    for(int i = 0; i < cntChoice; i++)
                    {
                        //if the checkbox is not clicked then click it
                        if(!mListView.isItemChecked(i))

                            {
                                //cb.performClick(); // didnt work

                                //cb.setChecked(true); // didnt work
                            }
                    }

任何人都可以清楚地向我解释(我是Android和Java的新手)如何更改我的适配器,以及如何在我的主要活动的OnClick Listener中实现它?

我是否必须为此创建一个新的ImageAdapter并将其重新绑定到ListView?

由于

我的适配器:

public class ImageAdapter extends BaseAdapter {
    Context context;
    ArrayList<Bitmap> images;
    ArrayList folderName;
    boolean[] checkBoxState;


    public ImageAdapter(Context c, ArrayList<Bitmap> images, ArrayList folderName){
        this.context = c;
        this.images = images;
        this.folderName = folderName;
        this.context = context;

        checkBoxState=new boolean[images.size()];
        imageLoader = ImageLoader.getInstance();

    }


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


    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    private class ViewHolder {
        TextView title;
        ImageView iconImage;
        CheckBox checkbox;
    }

 public View getView(final int position, View arg1, ViewGroup arg2) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View v = arg1;
        ViewHolder holder;

    if (arg1 == null) {
        LayoutInflater vi = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.list_row, null);

        holder = new ViewHolder();
        holder.title = (TextView) v.findViewById(R.id.filename);
         holder.iconImage = (ImageView) v.findViewById(R.id.list_image);
        holder.checkbox = (CheckBox)v.findViewById(R.id.checkBox1);

        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }
    int i = folderName.get(position).toString().lastIndexOf('\\');
    if(i!= -1)
    {
    holder.title.setText(folderName.get(position).toString().substring(i));
    }
    else
    {
        holder.title.setText(folderName.get(position).toString());
    }

    holder.iconImage.setImageBitmap(images.get(position));
    holder.checkbox.setChecked(checkBoxState[position]);
    holder.checkbox.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if(((CheckBox)v).isChecked())
                checkBoxState[position]=true;
            else
                checkBoxState[position]=false;

        }
    });

        return v;

2 个答案:

答案 0 :(得分:0)

首先,您需要向ViewHolder添加一个参数。

int id 

以下是getView()方法的更新

public View getView(final int position, View arg1, ViewGroup arg2) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View v = arg1;
        final ViewHolder holder;

        if (arg1 == null) {
            LayoutInflater vi = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_row, null);

            holder = new ViewHolder();
            holder.title = (TextView) v.findViewById(R.id.filename);
            holder.iconImage = (ImageView) v.findViewById(R.id.list_image);
            holder.checkbox = (CheckBox) v.findViewById(R.id.checkBox1);

            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }
        holder.id = position;
        int i = folderName.get(position).toString().lastIndexOf('\\');
        if (i != -1) {
            holder.title.setText(folderName.get(position).toString()
                    .substring(i));
        } else {
            holder.title.setText(folderName.get(position).toString());
        }

        holder.iconImage.setImageBitmap(images.get(position));
        holder.checkbox.setChecked(checkBoxState[position]);
        holder.checkbox
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton arg0,
                            boolean isCheck) {
                        if (isCheck)
                            checkBoxState[holder.id] = true;
                        else
                            checkBoxState[holder.id] = false;
                        notifyDataSetChange();
                    }
                });

        return v;
    }

请尽快告诉我结果

答案 1 :(得分:0)

首先,您需要将ImageAdapter adapter声明为全局变量

其次,您在

中使用了适配器
protected void onPostExecute(Boolean result) {
           adapter = new ImageAdapter(this, pix, paths);
           lstView.setAdapter(adapter);
        }

第三,在ImageAdapter类中再添加一个方法

public void selectedAll() {
    for(int i = 0; i< checkBoxState.length; i++){
       checkBoxState[i] = true;
    }
    notifyDataSetChanged();
}

最后,在select all click事件中调用previous方法。

if(adapter != null)
    adapter.selectedAll();