【问题标题】:How to fix Listview with check box?如何使用复选框修复 Listview?
【发布时间】:2017-03-20 14:58:59
【问题描述】:

我有一个带有复选框的列表视图,如果向上或向下滚动,复选框将变为未选中状态。我怎样才能解决这个问题?

这是我的列表视图适配器:

    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;

    }

    }


}

【问题讨论】:

标签: android listview checkbox android-adapter


【解决方案1】:

您是第 12345 号遇到此问题的人。您的问题(列表项的回收)已在 stackoverflow 上多次报告。所以只需谷歌一下解决方案。给你一个提示:你应该添加一个布尔数组,指示每个复选框的选中状态。并根据位置的数组值设置 getView() 中的复选框。在复选框的 onClickListener 中,将相应数组项的值设置为选中状态。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多