0

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;
user3437721
  • 2,227
  • 4
  • 31
  • 61
  • Try this - http://stackoverflow.com/questions/13746871/selecting-all-checkboxes-of-listview-in-android – Atul O Holic Apr 09 '14 at 14:54
  • I saw that already but didnt understand it (when they talk about the Model stuff) – user3437721 Apr 09 '14 at 14:55
  • Sure I can explain that. First can you post your code for, folderName and images? – Atul O Holic Apr 09 '14 at 15:01
  • They are just arrays. folderName is a String ArrayList which populates the textview. images is a Bitmap ArrayList which populates the imageviews in the custom list. They are built up in a doInBackground Async Task, and passed to the onPostExecute. – user3437721 Apr 09 '14 at 15:04
  • Ok great. Now what you need is to create a Model class (also known as POJO) which will hold probably three variables (String title, Bitmap and isChecked). Then create there getter and setter methods. After this change the code where you are setting your arrays to use the newly created setter methods and inside your getView fetch them using the getters. See few tutorials in case you are not clear. Post this we can move on the actual requirement. – Atul O Holic Apr 09 '14 at 15:13

2 Answers2

0

First, you need to add one more parameter to your ViewHolder.

int id 

Here are update for your getView() method

public View getView(final int position, View arg1, ViewGroup arg2) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View v = arg1;
        final 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();
        }
        holder.id = position;
        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
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton arg0,
                            boolean isCheck) {
                        if (isCheck)
                            checkBoxState[holder.id] = true;
                        else
                            checkBoxState[holder.id] = false;
                        notifyDataSetChange();
                    }
                });

        return v;
    }

Please let me know the result asap

Liem Vo
  • 5,149
  • 2
  • 18
  • 16
0

First you need to declare ImageAdapter adapter as global variable

Second, you used adapter in your

protected void onPostExecute(Boolean result) {
           adapter = new ImageAdapter(this, pix, paths);
           lstView.setAdapter(adapter);
        }

Third, Add one more method in your ImageAdapter class

public void selectedAll() {
    for(int i = 0; i< checkBoxState.length; i++){
       checkBoxState[i] = true;
    }
    notifyDataSetChanged();
}

Final, call previous method in your select all click event.

if(adapter != null)
    adapter.selectedAll();
Liem Vo
  • 5,149
  • 2
  • 18
  • 16
  • thanks, this makes more sense - where should I add the selectedAll() method in the image adapter class, does it need to go inside the GetView(), or outside of it? – user3437721 Apr 09 '14 at 15:45
  • selectedAll() is need to add out site Getview(). You have list view of your item and one more button for select all – Liem Vo Apr 09 '14 at 15:48
  • also I'm getting an error "Cannot resolve methiod notifyDataSetChange() in the selectedAll method – user3437721 Apr 09 '14 at 15:48
  • Correct method id notifyDataSetChanged(); – Liem Vo Apr 09 '14 at 15:49