从 FirebaseRecyclerAdapter 获取文档 ID 并将值传递到新页面

我有一个显示文档列表的 FirestoreRecyclerAdapter。我在列表中的每个项目旁边都有一个按钮。单击此按钮时,我想获取所选项目的唯一 ID,然后将用户带到显示该项目的个人详细信息的页面。


本质上,我想将单击的文档的 ID 传递到下一页。但是,我的按钮在单击时会引发空指针异常。


这段代码来自我的 ClubAdapter 类。


public class ClubAdapter1 extends FirestoreRecyclerAdapter<Club, ClubAdapter1.ClubHolder> {


    private String clubId;

    private Context context;


    public ClubAdapter1(@NonNull FirestoreRecyclerOptions<Club> options) {

        super(options);

    }


    @Override

    protected void onBindViewHolder(@NonNull ClubHolder holder, int position, @NonNull Club model) {

        holder.lblName.setText(model.getClubName());

        //getting the ID of the particular event clicked

        clubId = getSnapshots().getSnapshot(position).getId();


        holder.btnDetails.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                Intent i = new Intent(context, ViewSingleClub.class);

                i.putExtra("id", clubId);

                context.startActivity(i);

            }

        });

    }


    @NonNull

    @Override

    public ClubHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.clublist_layout, parent, false);

        return new ClubHolder(v);

    }


    class ClubHolder extends RecyclerView.ViewHolder {


        TextView lblName;

        Button btnDetails;


        public ClubHolder(@NonNull View itemView) {

            super(itemView);


            lblName = itemView.findViewById(R.id.lblName);

            btnDetails = itemView.findViewById(R.id.btnDetails);

        }

    }

}

我不断收到错误, Intent i = new Intent(context, ViewSingleClub.class); 我认为这可能与我的上下文变量引用自身有关,但也不确定我是否正确获取了 ID。有人能帮忙吗?




慕勒3428872
浏览 83回答 2
2回答

白衣染霜花

这是因为您正在分配“自身的上下文”。从初始化适配器的类中传递上下文。假设我有名为“ClubAdapter”的适配器和名为“ClubClass”的调用类在俱乐部类:ClubAdapter clubAdapter = new ClubAdapter(list, context);// if you'r calling it from activity then pass "Activity.this" and if fragment then "getContext()" as context.并在 ClubAdapterpublic class ClubAdapter extends RecyclerView.Adapter<ClubAdapter.ClubHolder> {&nbsp; &nbsp; private Context context;&nbsp; &nbsp; private List<String> list;&nbsp; &nbsp;&nbsp; &nbsp; public ClubAdapter(List<String> list, Context context) {&nbsp; &nbsp; &nbsp; &nbsp; this.list = list;&nbsp; &nbsp; &nbsp; &nbsp; this.context = context;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; @NonNull&nbsp; &nbsp; @Override&nbsp; &nbsp; public ClubHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {&nbsp; &nbsp; &nbsp; &nbsp; View itemView = LayoutInflater.from(viewGroup.getContext())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .inflate(R.layout.item_sample, viewGroup, false);&nbsp; &nbsp; &nbsp; &nbsp; return new ClubHolder(itemView, context);&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public class ClubHolder extends RecyclerView.ViewHolder {&nbsp; &nbsp; &nbsp; &nbsp; private Context context;&nbsp; &nbsp; &nbsp; &nbsp; private ClunHolder(@NonNull View itemView,Context context) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(itemView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.context = context;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }}或者你可以制作这样的界面。public interface RecycleViewOnItemClickListener {&nbsp; &nbsp; void onItemClick(int position);}并将其从调用类传递给您的适配器。假设我有名为“SampleAdapter”的适配器并调用名为“SampleClass”的类在“SampleClass”中,您可以将适配器中的接口作为参数传递。SampleAdapter sampleAdapter = new SampleAdapter(list, new RecycleViewOnItemClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onItemClick(int position) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // do what you want to perform&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });在“俱乐部适配器”中public class ClubAdapter extends RecyclerView.Adapter<ClubAdapter.ClubHolder> {&nbsp; &nbsp; &nbsp; &nbsp; private RecycleViewOnItemClickListener recycleViewOnItemClickListener;&nbsp; &nbsp; &nbsp; &nbsp; private List<String> list;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; public ClubAdapter(List<String> list, RecycleViewOnItemClickListener recycleViewOnItemClickListener) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.list = list;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.recycleViewOnItemClickListener = recycleViewOnItemClickListener;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; @NonNull&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public ClubHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; View itemView = LayoutInflater.from(viewGroup.getContext())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .inflate(R.layout.item_sample, viewGroup, false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new ClubHolder(itemView, recycleViewOnItemClickListener);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; public class ClubHolder extends RecyclerView.ViewHolder {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private RecycleViewOnItemClickListener recycleViewOnItemClickListener;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private ClunHolder(@NonNull View itemView,RecycleViewOnItemClickListener recycleViewOnItemClickListener) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(itemView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.recycleViewOnItemClickListener = recycleViewOnItemClickListener;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//you can callback onItemClick method from here on button click event so it give callback in "ClubActivity".&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;btn.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recycleViewOnItemClickListener.onItemClick(getAdapterPosition());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

慕的地6264312

要解决此问题,请在方法中移动以下代码行,onBindViewHolder()如下所示:holder.btnDetails.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; Intent i = new Intent(context, ViewSingleClub.class);&nbsp; &nbsp; &nbsp; &nbsp; i.putExtra("id", clubId);&nbsp; &nbsp; &nbsp; &nbsp; context.startActivity(i);&nbsp; &nbsp; }});看,我在holder.btnDetails.编辑:根据您的意见,还请进行以下操作。将构造函数更改为:public ClubAdapter1(@NonNull FirestoreRecyclerOptions<Club> options, Context context) {&nbsp; &nbsp; super(options);&nbsp; &nbsp; this.context = context;}当您在活动中创建适配器类的对象时,还将 传递context给构造函数。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java