如何停止ListView回收?

时间:2014-11-15 05:47:46

标签: android listview scroll onclick recycle

我是Android初学者,我无法弄清楚为什么会这样。

活动截图:

enter image description here

一切正常,除非我向下滚动(因此我认为它与回收有关)...所以当我向上滚动并试图撤消第一篇文章的投票(红色箭头)时,它认为帖子被投票了!

或者,它也可能认为向下投票的ImageButton drawable是空的 - 参见代码)。如果我不滚动,它可以很好地工作。

getView代码:

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        // inflate the list item
        convertView = this.inflater.inflate(R.layout.row_layout, parent, false);            
        // get views 
        holder.profilePic = (ImageView) convertView.findViewById(R.id.profilePic);
        holder.username = (TextView) convertView.findViewById(R.id.username);
        holder.day = (TextView) convertView.findViewById(R.id.day);
        holder.rating = (TextView) convertView.findViewById(R.id.rating);
        holder.textPost = (TextView) convertView.findViewById(R.id.textPost);
        holder.ratingUp = (ImageButton) convertView.findViewById(R.id.ratingUp);
        holder.ratingDown = (ImageButton) convertView.findViewById(R.id.ratingDown);
        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    final Drawable up_clicked = context.getResources().getDrawable(R.drawable.ic_action_rate_up_clicked);
    final Drawable up_unClicked = context.getResources().getDrawable(R.drawable.ic_action_rate_up);
    final Drawable down_clicked = context.getResources().getDrawable(R.drawable.ic_action_rate_down_clicked);
    final Drawable down_unClicked = context.getResources().getDrawable(R.drawable.ic_action_rate_down);

    Post post = cityFeed.get(position);
    holder.profilePic.setImageResource(post.getDrawableID());
    holder.username.setText(post.getUsername());
    holder.day.setText(post.getDay());
    holder.rating.setText(post.getRating());
    holder.textPost.setText(post.getText());        

    db = new DatabaseHandler(context);
    String userVotesUp = null;
    userVotesUp = db.getUserVotes("up");
    if (userVotesUp == null) {
        userVotesUp = "";
    }
    String userVotesDown = null;
    userVotesDown = db.getUserVotes("down");
    if (userVotesDown == null) {
        userVotesDown = "";
    }
    String postID = post.getPostID();
    if (userVotesUp.contains(postID)) {
        holder.ratingUp.setImageDrawable(up_clicked);
    } else if (userVotesDown.contains(postID) && userVotesUp != null) {
        holder.ratingDown.setImageDrawable(down_clicked);
    } else {
        holder.ratingUp.setImageDrawable(up_unClicked);
        holder.ratingDown.setImageDrawable(down_unClicked);
    }
    db.close();

    holder.ratingUp.setTag(position);
    holder.ratingDown.setTag(position);

    holder.ratingUp.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View convertView) {

            // get post details using button tag
            int pos = (Integer)convertView.getTag();
            Post post = cityFeed.get(pos);
            String postID = post.getPostID();
            String ratingString = post.getRating();
            int ratingValue = Integer.parseInt(ratingString);               
            RelativeLayout parent = (RelativeLayout)convertView.getParent().getParent();
            TextView ratingView = (TextView)parent.findViewById(R.id.rating);
            ImageButton up = (ImageButton)parent.findViewById(R.id.ratingUp);
            ImageButton down = (ImageButton)parent.findViewById(R.id.ratingDown);

            // if the post is not voted down...
            if (down.getDrawable() == down_unClicked) {             
                // if the post is not voted up...
                if (up.getDrawable() == up_unClicked) { 
                    up.setImageDrawable(up_clicked);
                    ratingValue = ratingValue + 1;
                    ratingString = Integer.toString(ratingValue);
                    ratingView.setText(ratingString);           
                    post.setRating(ratingString);                   
                    wsAsync = new WebServiceAsync(context);
                    wsAsync.execute(voteTag, postID, "up");                     
                // else the post is voted up...
                } else {
                    up.setImageDrawable(up_unClicked);
                    ratingValue = ratingValue - 1;
                    ratingString = Integer.toString(ratingValue);
                    ratingView.setText(ratingString);
                    post.setRating(ratingString);
                    wsAsync = new WebServiceAsync(context);
                    wsAsync.execute(voteTag, postID, "down");
                }                           
            // else the post is voted down...
            } else {
                down.setImageDrawable(down_unClicked);
                up.setImageDrawable(up_clicked);
                ratingValue = ratingValue + 2;
                ratingString = Integer.toString(ratingValue);
                ratingView.setText(ratingString);
                post.setRating(ratingString);
                wsAsync = new WebServiceAsync(context);
                wsAsync.execute(voteTag, postID, "upDownAlready");
            }
        }
    });

    holder.ratingDown.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View convertView) {

            // get post details using button tag
            int pos = (Integer)convertView.getTag();
            Post post = cityFeed.get(pos);
            String postID = post.getPostID();
            String ratingString = post.getRating();
            int ratingValue = Integer.parseInt(ratingString);               
            RelativeLayout parent = (RelativeLayout)convertView.getParent().getParent();
            TextView ratingView = (TextView)parent.findViewById(R.id.rating);
            ImageButton up = (ImageButton)parent.findViewById(R.id.ratingUp);
            ImageButton down = (ImageButton)parent.findViewById(R.id.ratingDown);

            // if the post is not voted up...
            if (up.getDrawable() == up_unClicked) {             
                // if the post is not voted down...
                if (down.getDrawable() == down_unClicked) { 
                    down.setImageDrawable(down_clicked);
                    ratingValue = ratingValue - 1;
                    ratingString = Integer.toString(ratingValue);
                    ratingView.setText(ratingString);           
                    post.setRating(ratingString);                   
                    wsAsync = new WebServiceAsync(context);
                    wsAsync.execute(voteTag, postID, "down");                       
                // else the post is voted down...
                } else {
                    down.setImageDrawable(down_unClicked);
                    ratingValue = ratingValue + 1;
                    ratingString = Integer.toString(ratingValue);
                    ratingView.setText(ratingString);
                    post.setRating(ratingString);
                    wsAsync = new WebServiceAsync(context);
                    wsAsync.execute(voteTag, postID, "up");
                }                           
            // else the post is voted up...
            } else {
                up.setImageDrawable(up_unClicked);
                down.setImageDrawable(down_clicked);
                ratingValue = ratingValue - 2;
                ratingString = Integer.toString(ratingValue);
                ratingView.setText(ratingString);
                post.setRating(ratingString);
                wsAsync = new WebServiceAsync(context);
                wsAsync.execute(voteTag, postID, "downUpAlready");
            }
        }
    });
    return convertView;
}

1 个答案:

答案 0 :(得分:2)

考虑到你的onClickListerners(在holder.ratingUp和holder.ratingDown上)工作正常,我认为问题出现在代码的这一部分

if (userVotesUp.contains(postID)) {
    holder.ratingUp.setImageDrawable(up_clicked);
} else if (userVotesDown.contains(postID) && userVotesUp != null) {
    holder.ratingDown.setImageDrawable(down_clicked);
} else {
    holder.ratingUp.setImageDrawable(up_unClicked);
    holder.ratingDown.setImageDrawable(down_unClicked);
}

如果我没有错,你做的就是你有三个条件,首先是

if (userVotesUp.contains(postID)) 

这意味着评级被提升并且您正在设置up_clicked imageDrawable,但您没有将down_unClicked image drawable设置为holder.ratingDown。

第二个条件适用于此

if (userVotesUp.contains(postID)) 

然后你将down_clicked drawable设置为holder.ratingDown,但是没有将up_unClicked image drawable设置为holder.ratingUp。

并且第三个条件既没有被投票也没有投票,这看起来很好。所以我想,你必须用这个

替换你的那部分代码
if (userVotesUp.contains(postID)) {
    holder.ratingUp.setImageDrawable(up_clicked);
    holder.ratingDown.setImageDrawable(down_unClicked);
} else if (userVotesDown.contains(postID) && userVotesUp != null) {
    holder.ratingDown.setImageDrawable(down_clicked);
    holder.ratingUp.setImageDrawable(up_unClicked);
} else {
    holder.ratingUp.setImageDrawable(up_unClicked);
    holder.ratingDown.setImageDrawable(down_unClicked);
}

希望这会有所帮助.. !!