2016-11-07 95 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狀態。