2016-11-07 90 views
-3

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

这里是我的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; 

    } 

    } 


} 
+0

[Android保存复选框状态在带有光标适配器的ListView中的可能的重复](http://stackoverflow.com/questions/2406937/android-save-checkbox-state-in-listview-with-cursor-adapter) – Adinia

回答

0

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