ViewHolder模式问题。为ArrayAdapter中的所有按钮设置相同的OnClickListener

时间:2014-12-24 08:39:23

标签: android listview android-arrayadapter onclicklistener android-viewholder

我无法弄清楚我错在哪里。我在自定义ListView适配器中创建了一个按钮,我已经实现了ViewHolder模式并为按钮设置了OnClickListener。

问题是,每当我点击任何一行的按钮时,我都会得到相同的点击结果。相同的标题,相同的用户名。

我还需要设置什么?谢谢,圣诞快乐!

自定义ListView适配器:

public FeedListViewAdapter(Context context, int layoutResourceId, ArrayList<FeedItemsSetter> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

static class ViewHolder {        
    ImageView image;
    public TextView postTitle;
    public TextView postCateg;
    public TextView userInfo;
    public TextView likerNames;
    public ImageView feedUserImage;
    public Button feedLike;
    public Button feedShare;
    public Button likeButton;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    //ViewHolder holder = null;
    // test as field, maybe problem
    item = data.get(position);

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        ViewHolder holder = new ViewHolder();

        holder.postTitle = (TextView) row.findViewById(R.id.postTitle);
        holder.postCateg = (TextView) row.findViewById(R.id.postCateg);
        holder.userInfo = (TextView) row.findViewById(R.id.feedUserName);
        holder.likerNames = (TextView) row.findViewById(R.id.likerNames);
        holder.feedUserImage = (ImageView) row.findViewById(R.id.feedUserImage);
        holder.image = (ImageView) row.findViewById(R.id.image);
        holder.feedLike = (Button) row.findViewById(R.id.feedLike);
        holder.feedShare = (Button) row.findViewById(R.id.feedShareBtn);
        holder.likeButton = (Button) row.findViewById(R.id.feedLike);

        row.setTag(holder);
    }

    ViewHolder holder = (ViewHolder) row.getTag();

    holder.postTitle.setText(item.getTitle());
    holder.postCateg.setText(item.getCategory());
    holder.userInfo.setText(item.getUser());
    holder.likerNames.setText(item.getLiker());
    holder.likerNames.setTag(item.getLikersLong());

    Picasso.with(context)
            .load("https://graph.facebook.com/" + item.getUserId()
                    + "/picture?type=large")
            .placeholder(R.drawable.placeholder)
            .error(R.drawable.placeholder)
            .transform(new RoundedTransformation(90, 4)).resize(100, 100)
            .centerCrop().into(holder.feedUserImage);

    Picasso.with(context)
            .load("http://www.edmondvarga.com/gastrolove/uploads/"
                    + item.getImage()).into(holder.image);

    holder.likerNames.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //mListener.onNamesClick(v.getTag().toString());
        }
    });

    holder.feedLike.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            System.out.println(item.getUser() + item.getTitle());

    });

    return row;
}

public void setCallback(facebookShareListener mListener) {
    this.mListener = mListener;
}

public static interface facebookShareListener {
    public void onSharePressed(String title, String imagename, String userId);

    public void onNamesClick(String title);
}

}

2 个答案:

答案 0 :(得分:2)

  

为ArrayAdapter

中的所有按钮设置相同的OnClickListener

使用ViewHolder的相同实例将setOnClickListener设置为:

 ViewHolder holder;
   if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new ViewHolder();
        holder.postTitle = (TextView) row.findViewById(R.id.postTitle);
        .....

        row.setTag(holder);
    }else{
      holder = (ViewHolder) row.getTag();
    }
     holder.postTitle.setText(item.getTitle());

答案 1 :(得分:1)

在if-else语句之外声明ViewHolder:

ViewHolder holder;

if (row == null) {
    holder = new ViewHolder();
    row.setTag(holder);
}else{
    holder = (ViewHolder) row.getTag();
}

将位置定义为最终位置,以便您可以在单击内访问特定项目索引:

public View getView(final int position, View convertView, ViewGroup parent) {
       holder.feedLike.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            System.out.println(data.get(position).getUser() + data.get(position).getTitle());
       });
}