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

时间:2019-02-06 12:06:31

标签: java android firebase google-cloud-firestore firebaseui

我有一个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。 有人可以帮忙吗?

这是我的错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.marykate.marykatefordefyp, PID: 2718
    java.lang.NullPointerException
        at android.content.ComponentName.<init>(ComponentName.java:77)
        at android.content.Intent.<init>(Intent.java:3842)
        at com.example.marykate.marykatefordefyp.ClubAdapter1$1.onClick(ClubAdapter1.java:36)
        at android.view.View.performClick(View.java:4457)
        at android.view.View$PerformClick.run(View.java:18491)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5336)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681)
        at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

要解决此问题,请在onBindViewHolder()方法内将以下代码行移动如下:

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

看,我在holder.btnDetails上添加了点击侦听器。

编辑:

根据您的评论,还请进行以下操作。将构造函数更改为:

public ClubAdapter1(@NonNull FirestoreRecyclerOptions<Club> options, Context context) {
    super(options);
    this.context = context;
}

然后在活动中创建适配器类的对象时,也将context传递给构造函数。

答案 1 :(得分:0)

这是因为您要分配“上下文本身”。从您初始化适配器的类传递上下文。

假设我有名称为“ ClubAdapter”的适配器,并且调用了名为“ ClubClass”的类

在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.
并在ClubAdapter

public class ClubAdapter extends RecyclerView.Adapter<ClubAdapter.ClubHolder> {
    private Context context;
    private List<String> list;
   

    public ClubAdapter(List<String> list, Context context) {
        this.list = list;
        this.context = context;
    }
    
    @NonNull
    @Override
    public ClubHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.item_sample, viewGroup, false);
        return new ClubHolder(itemView, context);
    }
    
    
    public class ClubHolder extends RecyclerView.ViewHolder {

        private Context context;

        private ClunHolder(@NonNull View itemView,Context context) {
            super(itemView);
            this.context = context;
           
        }
        }
}

或者您可以创建这样的界面。

public interface RecycleViewOnItemClickListener {
    void onItemClick(int position);
}

并将其从调用类传递到适配器。

假设我有一个名称为“ SampleAdapter”的适配器,并且调用了名为“ SampleClass”的类 在“ SampleClass”中,您可以将Adapter中的接口作为参数传递。

SampleAdapter sampleAdapter = new SampleAdapter(list, new RecycleViewOnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                // do what you want to perform
            }
        });

在“ ClubAdapter”中

public class ClubAdapter extends RecyclerView.Adapter<ClubAdapter.ClubHolder> {
        private RecycleViewOnItemClickListener recycleViewOnItemClickListener;
        private List<String> list;
       

        public ClubAdapter(List<String> list, RecycleViewOnItemClickListener recycleViewOnItemClickListener) {
            this.list = list;
            this.recycleViewOnItemClickListener = recycleViewOnItemClickListener;
        }
        
        @NonNull
        @Override
        public ClubHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View itemView = LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.item_sample, viewGroup, false);
            return new ClubHolder(itemView, recycleViewOnItemClickListener);
        }
        
        
        public class ClubHolder extends RecyclerView.ViewHolder {

            private RecycleViewOnItemClickListener recycleViewOnItemClickListener;

            private ClunHolder(@NonNull View itemView,RecycleViewOnItemClickListener recycleViewOnItemClickListener) {
                super(itemView);
                this.recycleViewOnItemClickListener = recycleViewOnItemClickListener;
               //you can callback onItemClick method from here on button click event so it give callback in "ClubActivity".
               btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    recycleViewOnItemClickListener.onItemClick(getAdapterPosition());
                }
            });
            }
            }
            }