gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:44:05 25 4
gpt4 key购买 nike

我不知道我哪里错了。我在我的自定义 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);
}

}

最佳答案

Same OnClickListener set for all buttons inside ArrayAdapter

使用相同的 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());

关于android - ViewHolder 模式问题。为 ArrayAdapter 内的所有按钮设置相同的 OnClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27633967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com