自定义列表视图,选中并取消选中Android中另一个CheckBox的所有CheckBox列?

时间:2016-03-15 16:44:45

标签: java android android-layout listview android-activity

我的适配器类中有两个CheckBox checkbox1,checkbox2。我希望如果我选中CheckBox,则应检查列表中的所有CheckBox。例如,如果我检查一个CheckBox,则ListView中有public class ListViewAdapter extends ArrayAdapter<Friend> { private List<Friend> myFriends; private Activity activity; private int selectedPosition = -1; public ListViewAdapter(Activity context, int resource, List<Friend> objects) { super(context, resource, objects); this.activity = context; this.myFriends = objects; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); // If holder not exist then locate all view from UI file. if (convertView == null) { // inflate UI from XML file convertView = inflater.inflate(R.layout.item_listview, parent, false); // get all UI view holder = new ViewHolder(convertView); // set tag for holder convertView.setTag(holder); } else { // if holder created, get tag from view holder = (ViewHolder) convertView.getTag(); } holder.checkBox1.setTag(position); // This line is important. holder.checkBox2.setTag(position+100); holder.friendName.setText(getItem(position).getName()); if (position == selectedPosition) { holder.checkBox.setChecked(true); } else holder.checkBox.setChecked(false); if( holder.checkBox1.getTag().equals(position)){ holder.checkBox1.setOnClickListener(onStateChangedListener(holder.checkBox1, position)); }else{ holder.checkBox2.setOnClickListener(onStateChangedListener(holder.checkBox2, position)); } return convertView; } private View.OnClickListener onStateChangedListener(final CheckBox checkBox, final int position) { return new View.OnClickListener() { @Override public void onClick(View v) { if (checkBox.isChecked()) { selectedPosition = position; } else { selectedPosition = -1; } notifyDataSetChanged(); } }; } private static class ViewHolder { private TextView friendName; private CheckBox checkBox1,checkBox2; public ViewHolder(View v) { checkBox1 = (CheckBox)v.findViewById(R.id.check); checkBox2=(CheckBox)v.findViewById(R.id.check1); friendName = (TextView) v.findViewById(R.id.name); } } } ,其他19个应该被检查。

此适配器类

unsigned char codeslink[5] ={ 0x33, 0x74, 0x74, 0x73, 0x72};
std::string my_std_string(reinterpret_cast<const char *>(codeslink), 5);
std::string my_std_string((const char *)codeslinks);

2 个答案:

答案 0 :(得分:0)

添加两个布尔标志。对象级isChecked1和模型中类级别(静态变量)isChecked2。默认为false。

我假设checkbox2是一次检查后将更改所有复选框以进行检查。

现在所有模型都会有isChecked1个变量。点击任意checkbox1,在点击的位置将模型中的isChecked1布尔值更改为true。并致电notifyDatasetChanged

如果用户检查checkbox2,只需将静态变量更改为true即可。并致电notifyDatasetChanged

getView方法中添加简单条件,如果isChecked2为true,则选中所有复选框,否则只检查模型为isChecked1的那些是否为真。

答案 1 :(得分:0)

为什么您没有为此目的使用可扩展列表视图而不是列表视图。

<ExpandableListView
      android:id="@+id/laptop_list"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
</ExpandableListView>
相关问题