问题描述
我是一个机器人初学者,我想不通为什么会这样。请帮忙!
活动截图:
一切正常,除了当我向下滚动(因此我认为这与回收做)......所以,当我向后滚动起来,试图撤消后的第一个投票(红色箭头)...它认为职位是否决!此外,它也可能认为向下票ImageButton的绘制是空的 - 见code)。如果我不滚动,它完美的作品。
getView code:
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
ViewHolder持有人;
如果(convertView == NULL){
持有人=新ViewHolder();
//充气列表项
convertView = this.inflater.inflate(R.layout.row_layout,父母,假);
//获取意见
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(保持器); }其他{
支架=(ViewHolder)convertView.getTag();
} 。最终绘制对象up_clicked = context.getResources()getDrawable(R.drawable.ic_action_rate_up_clicked);
最终绘制对象up_unClicked = context.getResources()getDrawable(R.drawable.ic_action_rate_up)。
最终绘制对象down_clicked = context.getResources()getDrawable(R.drawable.ic_action_rate_down_clicked)。
最终绘制对象down_unClicked = context.getResources()getDrawable(R.drawable.ic_action_rate_down)。 后后= cityFeed.get(位置);
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 =新数据库处理器(背景);
字符串userVotesUp = NULL;
userVotesUp = db.getUserVotes(上);
如果(userVotesUp == NULL){
userVotesUp =;
}
字符串userVotesDown = NULL;
userVotesDown = db.getUserVotes(下);
如果(userVotesDown == NULL){
userVotesDown =;
}
字符串postid = post.getPostID();
如果(userVotesUp.contains(帖子ID)){
holder.ratingUp.setImageDrawable(up_clicked);
}否则如果(userVotesDown.contains(帖子ID)及&放大器;!userVotesUp =空){
holder.ratingDown.setImageDrawable(down_clicked);
}其他{
holder.ratingUp.setImageDrawable(up_unClicked);
holder.ratingDown.setImageDrawable(down_unClicked);
}
db.close(); holder.ratingUp.setTag(位置);
holder.ratingDown.setTag(位置); holder.ratingUp.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(查看convertView){ //使用按钮标签后获得的详细信息
INT POS =(整数)convertView.getTag();
后后= cityFeed.get(POS)
字符串postid = post.getPostID();
串ratingString = post.getRating();
INT ratingValue =的Integer.parseInt(ratingString);
RelativeLayout的父=(RelativeLayout的)convertView.getParent()的getParent()。
TextView的ratingView =(TextView中)parent.findViewById(R.id.rating);
的ImageButton起来=(的ImageButton)parent.findViewById(R.id.ratingUp);
的ImageButton下来=(的ImageButton)parent.findViewById(R.id.ratingDown); //如果后期不否决?
如果(down.getDrawable()== down_unClicked){
//如果该信息是没有投票起来......
如果(up.getDrawable()== up_unClicked){
up.setImageDrawable(up_clicked);
ratingValue = ratingValue + 1;
ratingString = Integer.toString(ratingValue);
ratingView.setText(ratingString);
post.setRating(ratingString);
wsAsync =新WebServiceAsync(背景);
wsAsync.execute(voteTag,帖子ID,向上);
//否则职位是投了...
}其他{
up.setImageDrawable(up_unClicked);
ratingValue = ratingValue - 1;
ratingString = Integer.toString(ratingValue);
ratingView.setText(ratingString);
post.setRating(ratingString);
wsAsync =新WebServiceAsync(背景);
wsAsync.execute(voteTag,帖子ID,下);
}
//否则职位是否决...
}其他{
down.setImageDrawable(down_unClicked);
up.setImageDrawable(up_clicked);
ratingValue = ratingValue + 2;
ratingString = Integer.toString(ratingValue);
ratingView.setText(ratingString);
post.setRating(ratingString);
wsAsync =新WebServiceAsync(背景);
wsAsync.execute(voteTag,帖子ID,upDownAlready);
}
}
}); holder.ratingDown.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(查看convertView){ //使用按钮标签后获得的详细信息
INT POS =(整数)convertView.getTag();
后后= cityFeed.get(POS)
字符串postid = post.getPostID();
串ratingString = post.getRating();
INT ratingValue =的Integer.parseInt(ratingString);
RelativeLayout的父=(RelativeLayout的)convertView.getParent()的getParent()。
TextView的ratingView =(TextView中)parent.findViewById(R.id.rating);
的ImageButton起来=(的ImageButton)parent.findViewById(R.id.ratingUp);
的ImageButton下来=(的ImageButton)parent.findViewById(R.id.ratingDown); //如果该信息是没有投票起来......
如果(up.getDrawable()== up_unClicked){
//如果后期不否决?
如果(down.getDrawable()== down_unClicked){
down.setImageDrawable(down_clicked);
ratingValue = ratingValue - 1;
ratingString = Integer.toString(ratingValue);
ratingView.setText(ratingString);
post.setRating(ratingString);
wsAsync =新WebServiceAsync(背景);
wsAsync.execute(voteTag,帖子ID,下);
//否则职位是否决...
}其他{
down.setImageDrawable(down_unClicked);
ratingValue = ratingValue + 1;
ratingString = Integer.toString(ratingValue);
ratingView.setText(ratingString);
post.setRating(ratingString);
wsAsync =新WebServiceAsync(背景);
wsAsync.execute(voteTag,帖子ID,向上);
}
//否则职位是投了...
}其他{
up.setImageDrawable(up_unClicked);
down.setImageDrawable(down_clicked);
ratingValue = ratingValue - 2;
ratingString = Integer.toString(ratingValue);
ratingView.setText(ratingString);
post.setRating(ratingString);
wsAsync =新WebServiceAsync(背景);
wsAsync.execute(voteTag,帖子ID,downUpAlready);
}
}
});
返回convertView;
}
通过考虑您onClickListerners(上holder.ratingUp和holder.ratingDown)工作得很好,我认为这个问题是在code的这一部分
如果(userVotesUp.contains(帖子ID)){
holder.ratingUp.setImageDrawable(up_clicked);
}否则如果(userVotesDown.contains(帖子ID)及&放大器;!userVotesUp =空){
holder.ratingDown.setImageDrawable(down_clicked);
}其他{
holder.ratingUp.setImageDrawable(up_unClicked);
holder.ratingDown.setImageDrawable(down_unClicked);
}
如果我没看错,你正在做的事情一样,你有3个条件,首先是
如果(userVotesUp.contains(帖子ID))
这意味着评级upvoted,并要设置up_clicked imageDrawable,但你是不是down_unClicked图像绘制设置为holder.ratingDown。
第二个条件适用于本
如果(userVotesUp.contains(帖子ID))
那么你down_clicked绘制设置为holder.ratingDown,但up_unClicked图像绘制不设置holder.ratingUp。
和第三个条件是既不upvoted也不downvoted这似乎是罚款。所以我觉得,你有这个替换code你的那部分。
如果(userVotesUp.contains(帖子ID)){
holder.ratingUp.setImageDrawable(up_clicked);
holder.ratingDown.setImageDrawable(down_unClicked);
}否则如果(userVotesDown.contains(帖子ID)及&放大器;!userVotesUp =空){
holder.ratingDown.setImageDrawable(down_clicked);
holder.ratingUp.setImageDrawable(up_unClicked);
}其他{
holder.ratingUp.setImageDrawable(up_unClicked);
holder.ratingDown.setImageDrawable(down_unClicked);
}
希望这有助于.. !!
I'm an android beginner and I can't figure out why this is happening. Please help!
Activity Screenshot:
Everything works fine except when I scroll down (hence why I think it has to do with recycling)... So when I scroll back up and attempt to undo the vote (red arrow) on the first post... it thinks the post is voted down! Alternatively, it may also think the down-vote ImageButton drawable is empty - see code). If I don't scroll, it works perfectly.
getView code:
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;
}
By considering that your onClickListerners(on holder.ratingUp and holder.ratingDown) works fine, i think the problem is in this portion of your code
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 i am not wrong, you are doing something like that you have 3 conditions, First is
if (userVotesUp.contains(postID))
that means rating is upvoted and you are setting up_clicked imageDrawable, but you are not setting down_unClicked image drawable to holder.ratingDown.
second condition applies to this
if (userVotesUp.contains(postID))
then you are setting down_clicked drawable to holder.ratingDown, but not setting up_unClicked image drawable to holder.ratingUp.
and third condition is neither upvoted nor downvoted and that seems to be fine. so I think, you have to replace your that portion of code with this
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);
}
Hope this helps..!!
这篇关于如何停止的ListView回收?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!