gpt4 book ai didi

android - 如何停止 ListView 回收?

转载 作者:行者123 更新时间:2023-11-29 20:58:21 27 4
gpt4 key购买 nike

我是 Android 初学者,我不明白为什么会这样。

Activity 截图:

enter image description here

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

或者,它也可能认为否决票的 ImageButton 可绘制对象是空的 - 请参阅代码)。如果我不滚动,它会完美运行。

获取查看代码:

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;
}

最佳答案

考虑到您的 onClickListerners(on 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);
}

如果我没记错的话,你正在做的事情有 3 个条件,第一个是

if (userVotesUp.contains(postID)) 

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

第二个条件适用于此

if (userVotesUp.contains(postID)) 

然后您将 down_clicked 可绘制对象设置为 holder.ratingDown,但未将 up_unClicked 图像可绘制对象设置为 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);
}

希望这有帮助..!

关于android - 如何停止 ListView 回收?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26942994/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com