0

I have a ListFragment which populates lists with checkbox. Whenever i click on few checkbox some other checkbox is also getting selected automatically on same list.

public class ListViewDemoAdapter extends ArrayAdapter<ListViewItem> {


public ListViewDemoAdapter(Context context, List<ListViewItem> items) {
    super(context, R.layout.mytrip_item, items);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder viewHolder;

    if(convertView == null) {
        // inflate the GridView item layout
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.mytrip_item, parent, false);

        // initialize the view holder
        viewHolder = new ViewHolder();
        viewHolder.ivIcon                   = (ImageView) convertView.findViewById(R.id.ivIcon);
        viewHolder.tvTitle                  = (TextView) convertView.findViewById(R.id.tvTitle);
        viewHolder.tvDescription            = (TextView) convertView.findViewById(R.id.tvDescription);
        viewHolder.ratingBar                = (RatingBar) convertView.findViewById(R.id.ratingBar);

    // update the item view
    ListViewItem item = getItem(position);
    Glide.with(getContext())
    .load(NetworkIp.IMAGE_URL+item.pic_url)
    .into(viewHolder.ivIcon);

    viewHolder.tvTitle.setText(item.placename);
    viewHolder.tvDescription.setText(item.placedesc);
    viewHolder.ratingBar.setRating(item.placerate);
    return convertView;}
private static class ViewHolder {
    ImageView ivIcon;
    TextView tvTitle;
    TextView tvDescription;
    TextView tvDateTime;
    CheckBox chkBox;
    RatingBar ratingBar;
}

}

Ravi
  • 140
  • 1
  • 12

3 Answers3

2

You have to maintain a seperate hashMap for your check boxes to see whether they are selected or not because list view recycles its views.You can do like this:-

public class ListViewDemoAdapter extends ArrayAdapter<ListViewItem> {
    public HashMap<Integer,Boolean> chk;
    public ListViewDemoAdapter(Context context, List<ListViewItem> items) {
        super(context, R.layout.mytrip_item, items);
        chk = new HashMap<>();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;

        if(convertView == null) {
            // inflate the GridView item layout
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.mytrip_item, parent, false);

            // initialize the view holder
            viewHolder = new ViewHolder();
            viewHolder.ivIcon                   = (ImageView) convertView.findViewById(R.id.ivIcon);
            viewHolder.tvTitle                  = (TextView) convertView.findViewById(R.id.tvTitle);
            viewHolder.tvDescription            = (TextView) convertView.findViewById(R.id.tvDescription);
            viewHolder.ratingBar                = (RatingBar) convertView.findViewById(R.id.ratingBar);

            viewHolder.chkBox                   = (CheckBox) convertView.findViewById(R.id.chk);  ///  Your checkbox ID
            convertView.setTag(viewHolder); // setTag
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        // update the item view
        ListViewItem item = getItem(position);
        Glide.with(getContext())
                .load(NetworkIp.IMAGE_URL+item.pic_url)
                .into(viewHolder.ivIcon);

        viewHolder.tvTitle.setText(item.placename);
        viewHolder.tvDescription.setText(item.placedesc);
        viewHolder.ratingBar.setRating(item.placerate);
        viewHolder.chkBox.setTag(position);
        if(chk.containsKey(position)) {
            viewHolder.chkBox.setChecked(chk.get(position));
        }else{
            viewHolder.chkBox.setChecked(false);
            chk.put(position, false);
        }
        viewHolder.chkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                chk.put((Integer) buttonView.getTag(), isChecked);
            }
        });
        return convertView;
    }
    private static class ViewHolder {
        ImageView ivIcon;
        TextView tvTitle;
        TextView tvDescription;
        TextView tvDateTime;
        CheckBox chkBox;
        RatingBar ratingBar;
    }
}
Frosty
  • 500
  • 4
  • 14
  • Hey Thanks a lot @Frosty.. It worked.. Thanks a Lot. One more thing. how to fetch all checked ones ? – Ravi Feb 18 '16 at 06:21
  • Just iterate over your chk hashMap and look for checked ones. 'HashMap temp = new HashMap<>(chk); Iterator it = temp .entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry)it.next(); if((Boolean)pair.getValue()){ //here you get checked ones } it.remove(); // avoids a ConcurrentModificationException }' – Frosty Feb 18 '16 at 17:22
  • @Ravi If this solution worked for you then kindly accept and upvote my answer. – Frosty Feb 19 '16 at 21:33
  • I tried to upvote but stack was blocking me because I am new and i don't have 15 repo. – Ravi Feb 24 '16 at 09:00
0

The Checkbox belongs to a recycled view! You have to keep a separate array of "checked positions" in your fragment and update your checkboxes accordingly in your getView method.

JoeyJubb
  • 2,341
  • 1
  • 12
  • 19
0

Update your code. Get the CheckBox ID in your adapter and use setTag and getTag to implement recycleView.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder viewHolder;

    if(convertView == null) {
        // inflate the GridView item layout
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.mytrip_item, parent, false);

        // initialize the view holder
        viewHolder = new ViewHolder();
        viewHolder.ivIcon                   = (ImageView) convertView.findViewById(R.id.ivIcon);
        viewHolder.tvTitle                  = (TextView) convertView.findViewById(R.id.tvTitle);
        viewHolder.tvDescription            = (TextView) convertView.findViewById(R.id.tvDescription);
        viewHolder.ratingBar                = (RatingBar) convertView.findViewById(R.id.ratingBar);

        viewHolder.chkBox                   = (CheckBox) convertView.findViewById(R.id.chk);  ///  Your checkbox ID
        convertView.setTag(viewHolder); // setTag
    } else {
        viewHolder = (ViewHolder) convertView.getTag();    
    }

    // update the item view
    ListViewItem item = getItem(position);
    Glide.with(getContext())
    .load(NetworkIp.IMAGE_URL+item.pic_url)
    .into(viewHolder.ivIcon);

    viewHolder.tvTitle.setText(item.placename);
    viewHolder.tvDescription.setText(item.placedesc);
    viewHolder.ratingBar.setRating(item.placerate);
    return convertView;
    }
    private static class ViewHolder {
    ImageView ivIcon;
    TextView tvTitle;
    TextView tvDescription;
    TextView tvDateTime;
    CheckBox chkBox;
    RatingBar ratingBar;
   }
}
Rakesh
  • 756
  • 1
  • 9
  • 19
  • I could not see this checkbox setting in your adapter. viewHolder.chkBox = (CheckBox) convertView.findViewById(R.id.chk); Please check your code and define – Rakesh Feb 15 '16 at 10:06