-3

I have a listview with a check box, if scrolled up or down, the checkbox becomes unchecked. How can I fix this?

Here is my listviewadapter:

    String myisme = "1";
    private int SELF = 100;
    static final int CUSTOM_DIALOG_ID1 = 1;

    public FeedHomeAdapter(Context c, ArrayList<String> id, ArrayList<String> uname,
                           ArrayList<String> fname, ArrayList<String> time, ArrayList<String> status,
                           ArrayList<String> promo, ArrayList<String> ifliked, ArrayList<String> uid, ArrayList<String> pspic,
                           ArrayList<String> ppic, ArrayList<String> pcolor, ArrayList<String> slike, ArrayList<String> scomment,
                           ArrayList<String> slink, ArrayList<String> slinktext, ArrayList<String> sother,
                           ArrayList<String> sid, ArrayList<String> svideo, ArrayList<String> isfollow, ArrayList<String> ischat) {
        this.mContext = c;

        this.id = id;
        this.puName = uname;
        this.pfName = fname;
        this.ptime = time;
        this.psatus = status;
        this.ppromo = promo;
        this.pifliked = ifliked;
        this.puid = uid;
        this.pstatuspc = pspic;
        this.pprofilepc = ppic;
        this.pprofilecolor = pcolor;
        this.statuslike = slike;
        this.statuscomment = scomment;
        this.statuslink = slink;
        this.statuslinktext = slinktext;
        this.statusother = sother;
        this.statusid = sid;
        this.statusvideo = svideo;
        this.isfollowing = isfollow;
        this.pischat = ischat;

    }

    public int getCount() {
        // TODO Auto-generated method stub
        return id.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public long getItemId(int position) {

     return position;

    }

    public ArrayList<int[]> getSpans(String body, char prefix) {
        ArrayList<int[]> spans = new ArrayList<int[]>();

        Pattern pattern = Pattern.compile(prefix + "\\w+");
        Matcher matcher = pattern.matcher(body);

        // Check all occurrences
        while (matcher.find()) {
            int[] currentSpan = new int[2];
            currentSpan[0] = matcher.start();
            currentSpan[1] = matcher.end();
            spans.add(currentSpan);
        }

        return spans;
    }

    public View getView(final int pos, View child, ViewGroup parent) {
        final Holder mHolder;
        if (child == null) {
            child = LayoutInflater.from(mContext).inflate(R.layout.chat_display_item, parent, false);
            LayoutInflater layoutInflater;
            layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            child = layoutInflater.inflate(R.layout.card_layout, null);

            mHolder = new Holder();
            mHolder.txt_uName = (TextView) child.findViewById(R.id.name);
            mHolder.txt_uName1 = (TextView) child.findViewById(R.id.name1);
            mHolder.txt_fName = (TextView) child.findViewById(R.id.fname);
            mHolder.txt_satus = (TextView) child.findViewById(R.id.note);
            mHolder.txt_time = (TextView) child.findViewById(R.id.time);
            mHolder.txt_linktext = (TextView) child.findViewById(R.id.lint_title);
            mHolder.cunt_like = (TextView) child.findViewById(R.id.l_count);
            mHolder.like_text = (TextView) child.findViewById(R.id.like_text);
            mHolder.cunt_comment = (TextView) child.findViewById(R.id.c_count);
            mHolder.com_text = (TextView) child.findViewById(R.id.com_text);
            mHolder.txt_promo = (TextView) child.findViewById(R.id.promoted);
            mHolder.like = (ThumbUpView) child.findViewById(R.id.tpv2);
            mHolder.profile_pic = (CircleImageView) child.findViewById(R.id.profilePic);
            mHolder.status_pic = (ProportionalImageView) child.findViewById(R.id.status_pic);
            mHolder.video_cover = (RelativeLayout) child.findViewById(R.id.video_cover);
            mHolder.video_view = (FensterVideoView) child.findViewById(R.id.play_video_texture);
            mHolder.download = (LinearLayout) child.findViewById(R.id.download);
            mHolder.error_d = (LinearLayout) child.findViewById(R.id.error_d);
            mHolder.profile_view = (RelativeLayout) child.findViewById(R.id.id_to_profile);

            mHolder.comment = (ImageButton) child.findViewById(R.id.replide);

            child.setTag(mHolder);

        } else {
            mHolder = (Holder) child.getTag();
        }
        mHolder.txt_satus.setHighlightColor(Color.WHITE);
        mHolder.txt_satus.setHighlightColor(Color.TRANSPARENT);


        if (statuslike.get(pos).toString().equals("0")) {
            mHolder.like_text.setVisibility(View.GONE);
        } else {
            mHolder.cunt_like.setText(statuslike.get(pos));
        }

        if (statuscomment.get(pos).toString().equals("0")) {
            mHolder.com_text.setVisibility(View.GONE);
        } else {
            mHolder.cunt_like.setText(statuslike.get(pos));
            mHolder.cunt_comment.setText(statuscomment.get(pos));
        }




        mHolder.txt_linktext.setText(statuslinktext.get(pos));
        mHolder.txt_time.setText(ptime.get(pos));

        if (pifliked.get(pos).toString().equals("liked")) {
            //set liked
            mHolder.like.Like();
        } else {
            //set like
            mHolder.like.UnLike();
        }
        // init Effects class
        Effects.getInstance().init(mContext);

        mHolder.like.checkbox(new ThumbUpView.OnThumbUp() {
            @Override
            public void like(boolean like) {
                FeedCache controller = new FeedCache(mContext);
                dataBaseall = controller.getWritableDatabase();
                ContentValues values = new ContentValues();

                if (like) {
                    String yesliked = "liked";
                    values.put(FeedCache.KEY_IFLIKE, yesliked);
                    dataBaseall.update(FeedCache.TABLE_NAME, values, FeedCache.KEY_NOTEID + "= '" + statusid.get(pos) + "'", null);


                    mHolder.cunt_like.setText(String.valueOf(Integer.valueOf(mHolder.cunt_like.getText().toString()) + 1));
                    Effects.getInstance().playSound(Effects.SOUND_1);
                } else {
                    String yeslike = "like";
                    values.put(FeedCache.KEY_IFLIKE, yeslike);
                    dataBaseall.update(FeedCache.TABLE_NAME, values, FeedCache.KEY_NOTEID + "= '" + statusid.get(pos) + "'", null);

                    mHolder.cunt_like.setText(String.valueOf(Integer.valueOf(mHolder.cunt_like.getText().toString()) - 1));
                    Effects.getInstance().playSound(Effects.SOUND_1);

                }
                //close database
                dataBaseall.close();
            }
        });

 public class Holder {
        TextView txt_id;
        TextView txt_satus;
        TextView txt_time;
        TextView txt_uName;
        TextView txt_uName1;
        TextView txt_fName;
        TextView like_text;
        TextView cunt_like;
        TextView cunt_comment;
        TextView com_text;
        TextView txt_linktext;
        TextView txt_promo;
        ThumbUpView checkbox;

    }

    }


}
SiHa
  • 7,830
  • 13
  • 34
  • 43
  • Possible duplicate of [Android save Checkbox State in ListView with Cursor Adapter](http://stackoverflow.com/questions/2406937/android-save-checkbox-state-in-listview-with-cursor-adapter) – Adinia Nov 07 '16 at 12:28

1 Answers1

0

You are number 12345 with this problem. Your problem (recycling of list items) has been reported many times on stackoverflow. So just google a bit for the solution. To give you a hint: you should add a boolean array indicating the checked state of every checkbox. And set the checkbox in getView() according to array value for position. In an onClickListener for the checkbox set the value of the corrresponding array item to the checked state.

greenapps
  • 11,154
  • 2
  • 16
  • 19