how to select all checkboxes in custom list adapter programmatically?
friends,
i am using following custom adapter to show textview and checboxes in listview. now if textview is selectAll on clicking checbox infront of it in list i should select all checkboxes below in list. in my case selectall checkbox will be at position "0" and other below it.
any one guide me how to get all checkboxes array so that i could select or deselect them. any help would be appreciated.
public class EfficientAdapter extends BaseAdapter implements
Filterable {
private LayoutInflater mInflater;
//private Contex开发者_StackOverflowt context;
List<DalCategories> obj;
public EfficientAdapter(Context context,List<DalCategories> value) {
mInflater = LayoutInflater.from(context);
obj = value;
//this.context = context;
}
public View getView(final int position, View convertView,
final ViewGroup parent) {
ViewHolder holder;
convertView = mInflater.inflate(R.layout.list_category_item, null);
holder = new ViewHolder();
holder.TextTitle = (TextView) convertView.findViewById(R.id.list_item_title);
holder.checkBoxes = (CheckBox) convertView.findViewById(R.id.chkSubCategory);
if(obj.get(position).isIs_Selected())
{
holder.checkBoxes.setChecked(true);
}
holder.checkBoxes
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(
CompoundButton buttonView, boolean isChecked) {
if(select_all == true)
{
// select all here........
}
}
});
convertView.setTag(holder);
holder.TextTitle.setText(obj.get(position).getTitle());
return convertView;
}
class ViewHolder
{
public TextView TextTitle;
public CheckBox checkBoxes;
}
@Override
public Filter getFilter() {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public int getCount() {
return obj.size();
}
@Override
public Object getItem(int position) {
return obj.get(position);
}
}`enter code here`
To answer your main question... what you should do is iterate over the stored data (List obj) and set the values to true, and then call notifyDataSetChanged().
However there are many problems with your (somewhat ironically named) EfficientAdapter that you should address.
Firstly, you are using the ViewHolder pattern, but that's completely useless here because you're ignoring the convertView argument and creating a new view from scratch every time. Since your views never get reused, the viewholder doesn't do anything for you. You should only inflate a new view if convertView is null.
Secondly, it seems like you only have an action on click when the first item is clicked, but you're creating and setting handlers on every item. You could make your code more efficient by just moving that check outside rather than attaching an effectively no-op event handler to all but one of your elements.
I strongly suggest you go this video on ListViews from google I/O: http://developer.android.com/videos/index.html#
You need to have a global boolean array to store the states of the checkboxes.
Declare this array outside onCreate() method
private boolean[] itemToggled;
And Initialise it inside onCreate() method as below
itemToggled = new boolean[Checkbox.length];
int i;
for(i=0;i<Checkbox.length;i++)
{
itemToggled[i]=false;
}
Then you can have this inside your adapter.
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(select_all == true)
{
// select all here........
int i;
for(i=0;i<Checkbox.length;i++)
{
itemToggled[i]=true;
}
}
Use the value in this array for any handling of checkboxes.
selectAllButton.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
Log.i("Children Count",""+myListView.getCount()+"Items from MyList Checked");
for(int i=0;i<myListView.getCount();i++)
{
myListView.setItemChecked(i,true);
}
}
});
Enjoy and keep androiding...
精彩评论