I have managed to implement a custom Listview which has check boxes. The user can tap a row on the listview and the checkbox will check. The positions are stored even if a user scrolls.
The listview is bound to the adapter in the onPostExecute
of an Async task.
protected void onPostExecute(Boolean result) {
ImageAdapter adapter = new ImageAdapter(this, pix, paths);
lstView.setAdapter(adapter);
}
I can click on a list item and check the checkbox for that item like this:
lstView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long row) {
CheckBox cb = (CheckBox) view.findViewById(R.id.checkBox1);
cb.performClick();
}
I want to introduce a button which will check all the checkboxes.
I tried this but it is not working.
mSelectAllButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v.findViewById(R.id.checkBox1);//find the checkbox in the custom list
int cntChoice = mListView.getCount();
for(int i = 0; i < cntChoice; i++)
{
//if the checkbox is not clicked then click it
if(!mListView.isItemChecked(i))
{
//cb.performClick(); // didnt work
//cb.setChecked(true); // didnt work
}
}
Can anyone please explain to me clearly (I am new to Android and Java) how to change my adapter, and also how to implement this in an OnClick Listener from my main activity?
Do I have to create a new ImageAdapter for this and rebind it to the ListView?
Thanks
My Adapter:
public class ImageAdapter extends BaseAdapter {
Context context;
ArrayList<Bitmap> images;
ArrayList folderName;
boolean[] checkBoxState;
public ImageAdapter(Context c, ArrayList<Bitmap> images, ArrayList folderName){
this.context = c;
this.images = images;
this.folderName = folderName;
this.context = context;
checkBoxState=new boolean[images.size()];
imageLoader = ImageLoader.getInstance();
}
public int getCount() {
return images.size();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder {
TextView title;
ImageView iconImage;
CheckBox checkbox;
}
public View getView(final int position, View arg1, ViewGroup arg2) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = arg1;
ViewHolder holder;
if (arg1 == null) {
LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.title = (TextView) v.findViewById(R.id.filename);
holder.iconImage = (ImageView) v.findViewById(R.id.list_image);
holder.checkbox = (CheckBox)v.findViewById(R.id.checkBox1);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
int i = folderName.get(position).toString().lastIndexOf('\\');
if(i!= -1)
{
holder.title.setText(folderName.get(position).toString().substring(i));
}
else
{
holder.title.setText(folderName.get(position).toString());
}
holder.iconImage.setImageBitmap(images.get(position));
holder.checkbox.setChecked(checkBoxState[position]);
holder.checkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(((CheckBox)v).isChecked())
checkBoxState[position]=true;
else
checkBoxState[position]=false;
}
});
return v;