RecyclerView OnItemclick无法正常工作 [英] RecyclerView OnItemclick not working properly

查看:112
本文介绍了RecyclerView OnItemclick无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个回收站视图,并且正在其中实现OnClicklistener.基本上,我有一个名为actressadapter的适配器类和一个viewholder类MyViewHolder.我正在viewholder类内部实现OnClickListener以通过intent方法启动另一个活动.我的基本数据在一个名为actress的类中,该类具有三个变量名称,国家(均为字符串)和一个Id(UUID).我正在为适配器提供女演员的数据.下一个活动是只显示要检索的女演员的名字,如abc,def等.缺点是单击后仅显示一位女演员的名字.例如,如果显示abc,则每次单击都会显示abc.不知道为什么会这样,因为根据代码,我将actressname作为额外的值传递.

I have a recycler view and I am implementing OnClicklistener inside it. Basically , I have an adapter class called actressadapter and a viewholder class MyViewHolder.I am implementing OnClickListener inside viewholder class to initiate another activity via intent method. My basic data is inside a class called actress which has three variables name,country(both String) and an Id(UUID). I am providing this data that is of actresses to my adapter.Next activity is just displaying the name of actress like abc ,def etc that it retrieves . The fault is that it shows name of only one actress after clicking. For instance if it shows abc ,then for each click it will show abc.Don't know why is this happening because as per code I am passing actressname as an extra .

public class actressadapter extends RecyclerView.Adapter <actressadapter.MyViewHolder> {
private List<Actress>al;
private  Actress actress,mActress;
public static final String str="shivam.panwar.actressdetails.actressadapter.str";


public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public TextView naam, desh;

    public MyViewHolder(View view) {
        super(view);

        view.setOnClickListener(this);
        naam = (TextView) view.findViewById(R.id.name);
        desh= (TextView) view.findViewById(R.id.country);


    }
    @Override
    public void onClick(View v) {
        Toast.makeText(v.getContext(),"Clicked",Toast.LENGTH_SHORT).show();
    Intent intent=new Intent(v.getContext(),Actressview.class);

        intent.putExtra(str,actress.getName());
        v.getContext().startActivity(intent);
    }

    public void bindactress(Actress mActress) {
        naam.setText(mActress.getName());
        desh.setText(mActress.getCountry());
    }
}


public actressadapter(List<Actress> al) {
    this.al = al;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.actress_list_row, parent, false);


    return new MyViewHolder(itemView);


}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {


    actress =al.get(position);


    holder.bindactress(actress);
}



@Override
public int getItemCount() {
    return al.size();
}

}

如果需要有关代码的任何进一步帮助,请发表评论.

If required any further assist about code please comment.

推荐答案

您的问题是,您将actress设置为Adapte的变量,并且它应该是Holder的变量.

your issue is that you made actress a variable of the Adapte, and it should be of the Holder.

要修复它:

删除行:

 private  Actress actress,mActress;

onBindViewHolder方法更改为:

holder.bindactress(al.get(position))

并使用以下内容替换您的holder类:

And make replace your holder class with that:

// holder HAVE TO be static. It's WRONG not doing it static
public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView naam, desh;
    Actress actress;

    public MyViewHolder(View view) {
        super(view);
        view.setOnClickListener(this);
        naam = (TextView) view.findViewById(R.id.name);
        desh= (TextView) view.findViewById(R.id.country);

    }

    @Override
    public void onClick(View v) {
        Toast.makeText(v.getContext(),"Clicked",Toast.LENGTH_SHORT).show();
        Intent intent=new Intent(v.getContext(),Actressview.class);
        intent.putExtra(str,actress.getName());
        v.getContext().startActivity(intent);
    }

    public void bindactress(Actress mActress) {
        this.actress = mActress;
        naam.setText(mActress.getName());
        desh.setText(mActress.getCountry());
    }
}

这篇关于RecyclerView OnItemclick无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆