本文介绍了ViewHolder模式的问题。对于内部ArrayAdapter所有按钮一样OnClickListener集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法揣摩出我错了。我创建我的自定义的ListView适配器内部的一个按钮,我已经实现了ViewHolder模式,建立了一个OnClickListener的按钮。

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

要我需要设置什么?感谢和圣诞快乐!

自定义的ListView适配器:

 公共FeedListViewAdapter(上下文的背景下,INT layoutResourceId,ArrayList的< FeedItemsSetter>数据){
    超级(上下文,layoutResourceId,数据);
    this.layoutResourceId = layoutResourceId;
    this.context =背景;
    this.data =数据;
}静态类ViewHolder {
    ImageView的形象;
    公众的TextView postTitle;
    公众的TextView postCateg;
    公共TextView的用户信息;
    公众的TextView likerNames;
    公共ImageView的feedUserImage;
    公共按钮feedLike;
    公共按钮feedShare;
    公共按钮likeButton;
}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    查看排= convertView;
    // ViewHolder持有人= NULL;
    //作为测试领域,也许问题
    项目= data.get(位置);    如果(行== NULL){
        LayoutInflater充气=((活动)上下文).getLayoutInflater();
        行= inflater.inflate(layoutResourceId,父母,假);
        ViewHolder持有人=新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 =(按钮)row.findViewById(R.id.feedLike);
        holder.feedShare =(按钮)row.findViewById(R.id.feedShareBtn);
        holder.likeButton =(按钮)row.findViewById(R.id.feedLike);        row.setTag(保持器);
    }    ViewHolder支架=(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(上下文)
            .load(https://graph.facebook.com/+ item.getUserId()
                    +/图片?类型=大)
            .placeholder(R.drawable.placeholder)
            .error(R.drawable.placeholder)
            .transform(新RoundedTransformation(90,4))。调整(100,100)
            .centerCrop()成(holder.feedUserImage)。    Picasso.with(上下文)
            .load(http://www.edmondvarga.com/gastrolove/uploads/
                    + item.getImage())到(holder.image)。    holder.likerNames.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            。//mListener.onNamesClick(v.getTag()的toString());
        }
    });    holder.feedLike.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){            的System.out.println(item.getUser()+ item.getTitle());    });    返回行;
}公共无效setCallback(facebookShareListener mListener){
    this.mListener = mListener;
}公共静态界面facebookShareListener {
    公共无效onShare pressed(标题字符串,字符串imagename,字符串userid);    公共无效onNamesClick(字符串名称);
}}


解决方案

声明ViewHolder出方if-else语句:

  ViewHolder持有人;如果(行== NULL){
    持有人=新ViewHolder();
    row.setTag(保持器);
}其他{
    支架=(ViewHolder)row.getTag();
}

定义位置,最后让你可以访问内点击特定项目的指标:

 公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
       holder.feedLike.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            的System.out.println(data.get(位置).getUser()+ data.get(位置).getTitle());
       });
}

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);
}

}
解决方案

Declare ViewHolder out side 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());
       });
}

这篇关于ViewHolder模式的问题。对于内部ArrayAdapter所有按钮一样OnClickListener集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 17:58