0

我正在使用自定义适配器来管理列表视图。我在每一行都有一个按钮和文本视图。现在我想通过单击按钮添加喜欢-不喜欢的功能。但是当用户滚动列表时我遇到了问题,它回收了前一个列表项,当它再次出现时,它不会在 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;
}

}

4

0 回答 0