问题
I can't figure out where am I wrong. I have created a button inside my custom ListView Adapter, I have implemented the ViewHolder pattern and set up an OnClickListener for the button.
The problem is, whenever I click on the button on any row, I get the same result for clicks. Same title, same username.
What else to I need to set up? Thanks and Merry Christmas!
Custom ListView adapter:
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);
}
}
回答1:
Declare ViewHolder outside of the if-else statement :
ViewHolder holder;
if (row == null) {
holder = new ViewHolder();
row.setTag(holder);
}else{
holder = (ViewHolder) row.getTag();
}
Define position as final so you can access particular item index inside click :
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());
});
}
回答2:
Same OnClickListener set for all buttons inside ArrayAdapter
Use same instance of ViewHolder
for setting on setOnClickListener as:
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());
来源:https://stackoverflow.com/questions/27633967/viewholder-pattern-issue-same-onclicklistener-set-for-all-buttons-inside-arraya