简体   繁体   中英

Customized list view with Checked and unchecked all CheckBox columns by another CheckBox in android?

I have two CheckBox checkbox1, checkbox2 in my adapter class. I want that if I Check on CheckBox all CheckBox in the list should get checked. for example there are 20 CheckBox in my ListView if I check one, all other 19 should get checked.

this Adapter class

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);
    }
}
}

Add two Boolean flags. isChecked1 at object level and isChecked2 at class level(static variable) in your model. which are by default false.

I am assuming checkbox2 is the one which once checked will change all checkboxes to checked.

now all of you models will have isChecked1 variable. on click of any checkbox1 change the isChecked1 boolean in your model to true on the clicked position. and call notifyDatasetChanged .

if user checks on checkbox2 just change the static variable to true. and call notifyDatasetChanged .

In your getView method add simple condition that if isChecked2 is true make all checkbox checked otherwise only make those checked whose model has isChecked1 true.

Why you are not using Expandable list view instead of list view for this purpose.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM