我正在使用自定义适配器来管理listview。我每行都有一个按钮和textview。现在我想在点击按钮时添加喜欢不喜欢的功能。但是当用户滚动列表时我遇到问题,它会回收上一个列表项,当它再次出现时,它将不会在textview中显示类似的增量。
如何解决这个问题?有什么好的例子吗?
这里是适配器代码
public class ReviewAdapter extends BaseAdapter {
protected static final String TAG = "ReviewAdapter";
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
private HashMap<Integer, Integer> mCountHash;
public ReviewAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
mCountHash = new HashMap<Integer, Integer>();
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
/**
*
* @author
*
*/
public static class ViewReviewHolder {
TextView reviewLikeCount;
TextView reviewLikeText;
Button likeBtn;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
final ViewReviewHolder viewHolder;
if (vi == null) {
vi = inflater.inflate(R.layout.review_row, null);
viewHolder = new ViewReviewHolder();
viewHolder.reviewLikeCount = (TextView) vi
.findViewById(R.id.review_total_likes_count);
viewHolder.reviewLikeText = (TextView) vi
.findViewById(R.id.review_total_likes_text);
viewHolder.reviewCommentCount = (TextView) vi
.findViewById(R.id.review_total_comment_count);
viewHolder.likeBtn = (Button) vi.findViewById(R.id.review_like_btn);
vi.setTag(viewHolder);
} else {
viewHolder = (ViewReviewHolder) vi.getTag();
viewHolder.reviewLikeCount.setText("");
viewHolder.reviewLikeText.setVisibility(View.GONE);
}
// Like
// likeBtn.setTag(position);
HashMap<String, String> reviewData = new HashMap<String, String>();
reviewData = data.get(position);
// Setting all values in list view
// Like Count
if (reviewData.get(ListActivityReview.KEY_REVIEW_LIKE_COUNT).isEmpty()) {
mCountHash.put(position, 0);
} else {
mCountHash.put(position, Integer.parseInt(reviewData
.get(ListActivityReview.KEY_REVIEW_LIKE_COUNT)));
viewHolder.reviewLikeCount.setText(reviewData
.get(ListActivityReview.KEY_REVIEW_LIKE_COUNT));
viewHolder.reviewLikeCount.setVisibility(View.VISIBLE);
viewHolder.reviewLikeText.setVisibility(View.VISIBLE);
}
/*
* viewHolder.reviewLikeCount.setTag(position);
* viewHolder.likeBtn.setTag(position);
*/
viewHolder.likeBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Pos :" + position);
Log.d(TAG, "value " + mCountHash.get(position));
viewHolder.reviewLikeCount.setText(String.valueOf(mCountHash
.get(position) + 1));
viewHolder.reviewLikeCount.setVisibility(View.VISIBLE);
viewHolder.reviewLikeText.setVisibility(View.VISIBLE);
}
});
return vi;
}
答案 0 :(得分:1)
您是不是忘了在onClickListener中更新数据结构?
编辑:
viewHolder.likeBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int likeCount = Integer.parseInt( mCountHash.get(position) );
Log.d(TAG, "Pos :" + position);
Log.d(TAG, "value " + likeCount);
viewHolder.reviewLikeCount.setText(String.valueOf(likeCount + 1));
viewHolder.reviewLikeCount.setVisibility(View.VISIBLE);
viewHolder.reviewLikeText.setVisibility(View.VISIBLE);
// Update your data structure, so the data is correctly updated when the view is recycled and created again from datastructure
mCountHash.put( position, likeCount+1 );
}
});