选中所有复选框android listview

时间:2017-04-21 09:16:58

标签: android listview

我在每个项目中都有一个带有复选框的列表视图,当我点击我的复选框时,我需要检查列表视图中的所有复选框 checkbox_all 没有发生任何事情,这是我的代码,我不知道知道为什么它不起作用。

enter image description here

checkbox_all.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                for(int i = 0; i < list_view_themes.getAdapter().getCount(); i++){



LinearLayout itemLayout = (LinearLayout)list_view_themes.getAdapter().getView(i, null, list_view_themes);

                    if(itemLayout != null)
                    {
                        CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.checkbox_item);
                        cb.setChecked(b);
                      ((BaseAdapter) list_view_themes.getAdapter()).notifyDataSetChanged();
                    }
                }

            }
        });

这是我的适配器:

public class ConformityAdapter extends BaseAdapter {

private MainActivity mainActivity;

private List<Theme> themes;

public ConformityAdapter(MainActivity mainActivity, List<Theme> themes) {
    this.mainActivity = mainActivity;
    this.themes = themes;
}

@Override
public int getCount() {
    return themes.size();
}

@Override
public Object getItem(int i) {
    return themes.get(i);
}

@Override
public long getItemId(int i) {
    return 0;
}

@Override
public View getView(final int i, View convertView, ViewGroup viewGroup) {

    final ViewHolder holder;

    if (convertView == null) {

        holder = new ViewHolder();
        LayoutInflater inflater = mainActivity.getLayoutInflater();
        convertView = inflater.inflate(R.layout.listview_item, null);

        holder.text_title = (TextView) convertView.findViewById(R.id.text_title);
        holder.text_info= (ImageView) convertView.findViewById(R.id.info);
        holder.text_label = (TextView) convertView.findViewById(R.id.text_label);
        holder.text_message = (TextView) convertView.findViewById(R.id.text_message);
        holder.image_circle= (ImageView) convertView.findViewById(R.id.image_circle);
        holder.checkBox= (CheckBox) convertView.findViewById(R.id.checkbox_item);
        holder.line_listview_item = (View) convertView.findViewById(R.id.line_listview_item);
        holder.star_empty = (ImageView) convertView.findViewById(R.id.star_empty);


        convertView.setTag(holder);

    } else {

        holder = (ViewHolder) convertView.getTag();
    }
    holder.text_info.setVisibility(View.INVISIBLE);
    holder.text_label.setVisibility(View.GONE);
    holder.image_circle.setVisibility(View.GONE);
    holder.star_empty.setVisibility(View.GONE);
    holder.line_listview_item.setVisibility(View.INVISIBLE);
    holder.text_message.setPadding((int) Utils.dpToPixels(mainActivity, 20F),0,0,0);
    holder.text_title.setPadding((int) Utils.dpToPixels(mainActivity, 20F),0,0,0);

    holder.checkBox.setVisibility(View.VISIBLE);
    holder.text_title.setText(themes.get(i).getTitle());
    holder.text_label.setText("T");

    holder.text_message.setText(themes.get(i).getQuestions().size()+" Questions");
    holder.image_circle.setColorFilter(setColorFilter());

    convertView.findViewById(R.id.item).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            ConformityFragment c = new ConformityFragment();
            final int [] ids = new int[1];
            ids[0] = themes.get(i).getId();
            Bundle b = new Bundle();
            b.putIntArray("ids", ids);
            c.setArguments(b);
            ((MyApplication) mainActivity.getApplication()).setFragment(mainActivity,c, R.id.fragment_container);

        }
    });

    return convertView;
}
class ViewHolder {
    TextView text_title;
    TextView text_label;
    TextView text_message;
    ImageView text_info;
    ImageView image_circle;
    ImageView star_empty;
    CheckBox checkBox;
    View line_listview_item;


}
private int setColorFilter(){
    return ContextCompat.getColor(mainActivity, R.color.three_round);
}

}

1 个答案:

答案 0 :(得分:0)

这就是答案:

checkbox_all.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    list_view_themes.setAdapter(new ConformityAdapter((MainActivity) getActivity(), themes, b));
                    ((BaseAdapter) list_view_themes.getAdapter()).notifyDataSetChanged();

                }
            });

在我的适配器中,我添加了一个新的构造函数和一个新行

public ConformityAdapter(MainActivity mainActivity, List<Theme> themes, boolean b) {
        this.mainActivity = mainActivity;
        this.themes = themes;
        this.b = b;
    }

新行

    holder.checkBox.setChecked(b);