RecyclerView OnItemclick无法正常工作

时间:2016-11-09 17:41:55

标签: android android-intent android-recyclerview onclicklistener

我有一个回收站视图,我在其中实现了OnClicklistener。基本上,我有一个名为actressadapter的适配器类和一个viewholder类MyViewHolder。我在viewholder类中实现OnClickListener,通过intent方法启动另一个活动。我的基本数据是在一个名为actress的类中,它有三个变量 name,country(都是String)和一个Id(UUID)。我正在向我的adapter提供这个actress的数据.Next activity is is只显示它检索的abc,def等女演员的名字。故障是它在点击后只显示一位女演员的名字。例如,如果它显示abc,那么对于每次点击它将显示abc.Don不知道为什么会发生这种情况,因为根据代码我将actressname作为额外的传递。

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

}

Exporting An Indie Unity Game to WebVR

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

3 个答案:

答案 0 :(得分:1)

尝试这种希望它有效的方式...

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    actress =al.get(position);
    holder.bindactress(actress);
    holder.naam.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context,holder.naam.getText().toString(), Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(context,Actressview.class);
                    intent.putExtra(str,holder.naam.getText().toString());
                    context.startActivity(intent);
                }
            });

}

答案 1 :(得分:1)

您的问题是您将actress变为Adapte的变量,它应该是持有者。

修复它:

删除该行:

 private  Actress actress,mActress;

onBindViewHolder方法更改为:

holder.bindactress(al.get(position))

并用以下内容替换你的持有者类:

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

答案 2 :(得分:1)

使用适配器类的 getLayoutPosition()方法获取所选项目(位置)的最佳方法。

您必须更新代码才能获得更好的结果。用此替换现有代码。

public class actressadapter extends RecyclerView.Adapter <actressadapter.MyViewHolder> {
private List<Actress>al;
private  Actress actress,mActress;
private int itemPosition;  // change
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) {
            itemPosition = getLayoutPosition();   // change
            Toast.makeText(v.getContext(),"Clicked",Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(v.getContext(),Actressview.class);

            intent.putExtra(str,al.get(itemPosition).getName());  // change
            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();
    }
}

希望它能起作用..