Android - 适配器位置错误
Android - Adapter wrong position
我在OnClickListener中设置更改背景图标:
holder.rating_good.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
holder.rating_good.setBackgroundResource(R.drawable.ic_thumb_up_red);
apiMethods.ChangePostRating(ctx, String.valueOf(item.getId()), "up", holder.rating, holder.rating_good);
}
});
holder.rating_bad.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
holder.rating_good.setBackgroundResource(R.drawable.ic_thumb_down_red);
apiMethods.ChangePostRating(ctx, String.valueOf(item.getId()), "down", holder.rating, holder.rating_bad);
}
});
但是在某些项目背景发生变化时,即使我没有按下按钮。
标准背景 ImageView if this rating_good - R.drawable.ic_thumb_down_grey, if this rating_bad - R.drawable.ic_thumb_down_grey.
我的getView:
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.feed_item, parent, false);
holder.rating_good = (ImageView) convertView.findViewById(R.id.imageRatingGood);
holder.rating_bad = (ImageView) convertView.findViewById(R.id.imageRatingBad);
holder.rating = (TextView) convertView.findViewById(R.id.feedRating);
holder.rating.setTag(position);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try {
item = feedItems.get(position);
ctx = parent.getContext();
holder.rating_good.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
holder.rating_good.setBackgroundResource(R.drawable.ic_thumb_up_red);
apiMethods.ChangePostRating(ctx, String.valueOf(item.getId()), "up", holder.rating, holder.rating_good);
}
});
holder.rating_bad.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
holder.rating_good.setBackgroundResource(R.drawable.ic_thumb_down_red);
apiMethods.ChangePostRating(ctx, String.valueOf(item.getId()), "down", holder.rating, holder.rating_bad);
}
});
//OTHER SOME CODE
} catch (Exception e) {
e.printStackTrace();
Mint.logException(e);
}
return convertView;
}
static class ViewHolder {
TextView nickname;
TextView date;
TextView description;
TextView rating;
TextView type;
FeedImageView feedImageView;
ImageView rating_good;
ImageView rating_bad;
ImageView type_image;
LinearLayout linear;
}
ApiMethods 类:
public void ChangePostRating(Context new_ctx, String id, String rate, TextView textview, ImageView rView) {
ctx = new_ctx;
token = pref.GetToken(ctx);
content_id = id;
rating_string = rate;
url = apiUrl.GetUrlChangeRating();
app_id = info.app_key;
myView = textview;
imageButton = (ImageView) rView;
RequestQueue queue = Volley.newRequestQueue(ctx);
StringRequest sr = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String responses) {
try {
JSONObject response = new JSONObject(responses);
String status = response.getString("status");
if (!status.contains("error")) {
JSONObject data = response.getJSONObject("data");
int rating = data.getInt("rating");
TextView text_rating = (TextView) myView;
text_rating.setText(String.valueOf(rating));
if (Integer.valueOf(text_rating.getText().toString()) < 0) {
text_rating.setTextColor(ctx.getResources().getColor(R.color.feed_item_rating_bad));
} else {
text_rating.setTextColor(ctx.getResources().getColor(R.color.feed_item_rating_good));
}
} else {
JSONObject error = response.getJSONObject("error");
String message = error.getString("message");
parseError(message);
if (rating_string.contains("up")) {
imageButton.setBackgroundResource(R.drawable.ic_thumb_up_grey);
} else {
imageButton.setBackgroundResource(R.drawable.ic_thumb_down_grey);
}
}
} catch (JSONException e) {
Mint.logException(e);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Mint.logException(error);
}
});
queue.add(sr);
}
你应该只为每个视图订阅一次事件处理程序,如下所示(未经测试,因为我在这里输入它,但你明白了)。多次订阅它们会导致问题,因为您正在缓存它们(因此使用 ViewHolder)。
现在这是否能解决您的问题是另一回事,但它仍然是您需要解决的问题。
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
if (convertView == null) {
if (inflater == null)
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.feed_item, parent, false);
holder.rating_good = (ImageView) convertView.findViewById(R.id.imageRatingGood);
holder.rating_bad = (ImageView) convertView.findViewById(R.id.imageRatingBad);
holder.rating = (TextView) convertView.findViewById(R.id.feedRating);
holder.rating_good.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ViewHolder viewHolder = (ViewHolder)v.getTag()
viewHolder.rating_good.setBackgroundResource(R.drawable.ic_thumb_up_red);
apiMethods.ChangePostRating(ctx, String.valueOf(viewHolder.item.getId()), "up", viewHolder.rating, viewHolder.rating_good);
}
});
holder.rating_bad.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ViewHolder viewHolder = (ViewHolder)v.getTag()
viewHolder.rating_good.setBackgroundResource(R.drawable.ic_thumb_up_green);
apiMethods.ChangePostRating(ctx, String.valueOf(viewHolder.item.getId()), "down", viewHolder.rating, viewHolder.rating_bad);
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try {
item = feedItems.get(position);
ctx = parent.getContext();
holder._rating_bad.setTag(holder);
holder._rating_good.setTag(holder);
holder.rating.setTag(position);
holder._item = item;
});
//OTHER SOME CODE
} catch (Exception e) {
e.printStackTrace();
Mint.logException(e);
}
return convertView;
}
我在OnClickListener中设置更改背景图标:
holder.rating_good.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
holder.rating_good.setBackgroundResource(R.drawable.ic_thumb_up_red);
apiMethods.ChangePostRating(ctx, String.valueOf(item.getId()), "up", holder.rating, holder.rating_good);
}
});
holder.rating_bad.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
holder.rating_good.setBackgroundResource(R.drawable.ic_thumb_down_red);
apiMethods.ChangePostRating(ctx, String.valueOf(item.getId()), "down", holder.rating, holder.rating_bad);
}
});
但是在某些项目背景发生变化时,即使我没有按下按钮。 标准背景 ImageView if this rating_good - R.drawable.ic_thumb_down_grey, if this rating_bad - R.drawable.ic_thumb_down_grey.
我的getView:
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.feed_item, parent, false);
holder.rating_good = (ImageView) convertView.findViewById(R.id.imageRatingGood);
holder.rating_bad = (ImageView) convertView.findViewById(R.id.imageRatingBad);
holder.rating = (TextView) convertView.findViewById(R.id.feedRating);
holder.rating.setTag(position);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try {
item = feedItems.get(position);
ctx = parent.getContext();
holder.rating_good.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
holder.rating_good.setBackgroundResource(R.drawable.ic_thumb_up_red);
apiMethods.ChangePostRating(ctx, String.valueOf(item.getId()), "up", holder.rating, holder.rating_good);
}
});
holder.rating_bad.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
holder.rating_good.setBackgroundResource(R.drawable.ic_thumb_down_red);
apiMethods.ChangePostRating(ctx, String.valueOf(item.getId()), "down", holder.rating, holder.rating_bad);
}
});
//OTHER SOME CODE
} catch (Exception e) {
e.printStackTrace();
Mint.logException(e);
}
return convertView;
}
static class ViewHolder {
TextView nickname;
TextView date;
TextView description;
TextView rating;
TextView type;
FeedImageView feedImageView;
ImageView rating_good;
ImageView rating_bad;
ImageView type_image;
LinearLayout linear;
}
ApiMethods 类:
public void ChangePostRating(Context new_ctx, String id, String rate, TextView textview, ImageView rView) {
ctx = new_ctx;
token = pref.GetToken(ctx);
content_id = id;
rating_string = rate;
url = apiUrl.GetUrlChangeRating();
app_id = info.app_key;
myView = textview;
imageButton = (ImageView) rView;
RequestQueue queue = Volley.newRequestQueue(ctx);
StringRequest sr = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String responses) {
try {
JSONObject response = new JSONObject(responses);
String status = response.getString("status");
if (!status.contains("error")) {
JSONObject data = response.getJSONObject("data");
int rating = data.getInt("rating");
TextView text_rating = (TextView) myView;
text_rating.setText(String.valueOf(rating));
if (Integer.valueOf(text_rating.getText().toString()) < 0) {
text_rating.setTextColor(ctx.getResources().getColor(R.color.feed_item_rating_bad));
} else {
text_rating.setTextColor(ctx.getResources().getColor(R.color.feed_item_rating_good));
}
} else {
JSONObject error = response.getJSONObject("error");
String message = error.getString("message");
parseError(message);
if (rating_string.contains("up")) {
imageButton.setBackgroundResource(R.drawable.ic_thumb_up_grey);
} else {
imageButton.setBackgroundResource(R.drawable.ic_thumb_down_grey);
}
}
} catch (JSONException e) {
Mint.logException(e);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Mint.logException(error);
}
});
queue.add(sr);
}
你应该只为每个视图订阅一次事件处理程序,如下所示(未经测试,因为我在这里输入它,但你明白了)。多次订阅它们会导致问题,因为您正在缓存它们(因此使用 ViewHolder)。
现在这是否能解决您的问题是另一回事,但它仍然是您需要解决的问题。
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
if (convertView == null) {
if (inflater == null)
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.feed_item, parent, false);
holder.rating_good = (ImageView) convertView.findViewById(R.id.imageRatingGood);
holder.rating_bad = (ImageView) convertView.findViewById(R.id.imageRatingBad);
holder.rating = (TextView) convertView.findViewById(R.id.feedRating);
holder.rating_good.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ViewHolder viewHolder = (ViewHolder)v.getTag()
viewHolder.rating_good.setBackgroundResource(R.drawable.ic_thumb_up_red);
apiMethods.ChangePostRating(ctx, String.valueOf(viewHolder.item.getId()), "up", viewHolder.rating, viewHolder.rating_good);
}
});
holder.rating_bad.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ViewHolder viewHolder = (ViewHolder)v.getTag()
viewHolder.rating_good.setBackgroundResource(R.drawable.ic_thumb_up_green);
apiMethods.ChangePostRating(ctx, String.valueOf(viewHolder.item.getId()), "down", viewHolder.rating, viewHolder.rating_bad);
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try {
item = feedItems.get(position);
ctx = parent.getContext();
holder._rating_bad.setTag(holder);
holder._rating_good.setTag(holder);
holder.rating.setTag(position);
holder._item = item;
});
//OTHER SOME CODE
} catch (Exception e) {
e.printStackTrace();
Mint.logException(e);
}
return convertView;
}