如何让多个按钮进入firestore recyclerview?

3df52oht  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(400)

我设法让一个按钮工作(btninfo),但后来我没有线索,如何添加第二个(btninchrijven)。你知道怎么让第二个按钮工作吗?
我对编码很陌生,使用youtube和逻辑思维,但有些东西我就是找不到或想不出来。
这就是它的样子
适配器:

public  class LesViewHolder extends RecyclerView.ViewHolder{

    TextView Lijst_Soort, Lijst_Wanneer, Lijst_Waar;

    public LesViewHolder(@NonNull View itemView){
        super(itemView);

        Lijst_Soort = itemView.findViewById(R.id.idLes);
        Lijst_Waar = itemView.findViewById(R.id.idWaar);
        Lijst_Wanneer = itemView.findViewById(R.id.idWanneer);
        btnInfo = itemView.findViewById(R.id.btnInfo);
        btnInschrijven = itemView.findViewById(R.id.btnInschrijven);

        btnInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              int position = getAdapterPosition();
                if(position !=RecyclerView.NO_POSITION && listener !=null){
                    listener.onItemClick(getSnapshots().getSnapshot(position), position );
                }
            }
        });
}

 public LesAdapter getInstance() {
    return instance;
    }
    public interface OnItemClickListener{
    void onItemClick(DocumentSnapshot documentSnapshot, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener){
    this.listener = listener;

    }

主要片段:

adapter.setOnItemClickListener(new LesAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(DocumentSnapshot documentSnapshot, int position) {

            Timestamp wanneer = documentSnapshot.getTimestamp("Wanneer");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E d MMMM HH:mm");
            String dateTime = simpleDateFormat.format(wanneer.toDate());
            Intent intent = new Intent(getActivity(), Deelnamelijst.class);
            intent.putExtra("Welke les", dateTime);
            startActivity(intent);
        }
    });
cpjpxq1n

cpjpxq1n1#

用下面的代码更改您的代码

public interface OnItemClickListener{
     void onItemClick(DocumentSnapshot documentSnapshot, int position);
     void onSecondButtonClick(DocumentSnapshot documentSnapshot, int position);
     //you can add any number of methods here as per your requrenments
}

现在在第二个按钮上单击addlistener并执行此操作

btnInschrijven.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          int position = getAdapterPosition();
            if(position !=RecyclerView.NO_POSITION && listener !=null){  
              listener.onSecondButtonClick(getSnapshots().getSnapshot(position), position );
            }
        }
    });

现在在您的活动中,无论您在哪里分配 OnItemClickListener 您需要重写在中创建的新方法 interface 所以当你点击第二个按钮 onSecondButtonClick() 将执行,并且重写方法下的代码将得到execute

adapter.setOnItemClickListener(new LesAdapter.OnItemClickListener() {
    @Override
    public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
           //...
    }

    @Override
    public void onSecondButtonClick(DocumentSnapshot documentSnapshot, int position) {
          //todo here you get click
    }
});

相关问题