Reputation: 8362
I want to implement a scenario- There is present a checkbox on the top of the screen and there is a custom listview with checkboxes below that top checkbox.
I want that one someone checks that checkbox all the listview checkboxes should be checked and vice versa.
public class GroupMemberListAdapter extends BaseAdapter {
private LayoutInflater inflater = null;
Context Mycontext;
public GroupMemberListAdapter(Context context) {
Mycontext = context;
inflater = LayoutInflater.from(context);
}
public int getCount() {
return broadcastList.size();
}
public Object getItem(int paramInt) {
return paramInt;
}
public long getItemId(int paramInt) {
return paramInt;
}
public View getView(int position, View convertView, ViewGroup parent) {
EventViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.contactcustomlayout,
null);
holder = new EventViewHolder();
holder.mtvGroupMemberName = (TextView) convertView
.findViewById(R.id.tvGroupMemberName);
holder.checkbox_group_member = (CheckBox)convertView.findViewById(R.id.checkBox_GroupMember);
/*holder.mtvGroupMemberAbout = (TextView) convertView
.findViewById(R.id.tvGroupMemberAbout);*/
convertView.setTag(holder);
} else {
holder = (EventViewHolder) convertView.getTag();
}
holder.mtvGroupMemberName.setText(""+broadcastList.get(position));
//holder.mtvGroupMemberAbout.setText(""+data.get(position));
return convertView;
}
public class EventViewHolder {
private TextView mtvGroupMemberName;
private TextView mtvGroupMemberAbout;
private CheckBox checkbox_group_member;
}
}
checkBox_selectAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
checkAllBoxes();
}
});
private void checkAllBoxes(){
int size = mlvGroupBroadContact.getAdapter().getCount();
System.out.println("Size of the list is:"+size);
}
How can I check all the list view boxes???
Upvotes: 0
Views: 180
Reputation: 1550
Update a Boolean variable in your adapter class from on checkBox_selectAll click listner. and call view.getAdapter.notifyDataSetChanged();
In getView based on this variable set it state checked or unchecked.
Upvotes: 0
Reputation: 21181
you can use the below code to check all checkboxes in the listview
private void checkAllBoxes(){
int size = mlvGroupBroadContact.getAdapter().getCount();
System.out.println("Size of the list is:"+size);
CheckBox chk;
for(int i=0;i<size;i++)
{
chk= ((CheckBox)jobList.getChildAt(i).findViewById(R.id.chk));
chk.setChecked(true);
}
}
Upvotes: 1
Reputation: 29436
If your ListView
's choice mode is CHOICE_MODE_MULTIPLE
or CHOICE_MODE_SINGLE
:
private void deselectAll() {
getListView().clearChoices();
//--as of GingerBread, clearChoices() alone is not sufficient--
//--we'd have to manually un-check visible items--
for (int i = 0; i < getListView().getChildCount(); i++) {
View c = getListView().getChildAt(i);
if (c instanceof Checkable) {
((Checkable) c).setChecked(false);
}
}
}
private void selectAll() {
SparseBooleanArray sba = getListView().getCheckedItemPositions();
for (int i = 0; i < getAdapter().getCount(); i++) {
sba.put(i, true);
}
for (int i = 0; i < getListView().getChildCount(); i++) {
View c = getListView().getChildAt(i);
if (c instanceof Checkable) {
((Checkable) c).setChecked(true);
}
}
}
Replace getListView()
and getAdapter()
with your ListView
and its Adapter
instances.
Upvotes: 1