studio - reload list view android
¿Cómo detener el reciclaje de ListView? (1)
Al considerar que su onClickListerners (en holder.ratingUp y holder.ratingDown) funciona bien, creo que el problema está en esta parte de su código
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);
}
Si no estoy equivocado, estás haciendo algo así, tienes 3 condiciones, la primera es
if (userVotesUp.contains(postID))
eso significa que la calificación está subida y usted configura up_clicked imageDrawable, pero no está configurando down_unClicked image drawable para holder.ratingDown.
segunda condición se aplica a esto
if (userVotesUp.contains(postID))
luego está configurando down_clicked drawable para holder.ratingDown, pero no configurando up_unClicked imagen drawable para holder.ratingUp.
y la tercera condición no está subvitada ni degradada y eso parece estar bien. entonces creo que tienes que reemplazar tu porción de código con esto
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);
}
Espero que esto ayude..!!
Soy un principiante de Android y no puedo entender por qué sucede esto.
Captura de pantalla de la actividad:
Todo funciona bien, excepto cuando me desplazo hacia abajo (de ahí el motivo por el que creo que tiene que ver con el reciclaje) ... Así que cuando me desplazo hacia atrás e intento deshacer el voto (flecha roja) en la primera publicación, cree que la publicación ha sido votada ¡abajo!
Alternativamente, también puede pensar que el draw-down ImageButton dibujable está vacío, vea el código). Si no me desplazo, funciona perfectamente.
código 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;
}