Ive managed to get one button working (btnInfo), but then I had no clue, how to add the second one (btnInschrijven). Any Idea's on how to make the second button work?
I'm new to coding, using youtube and logic thinking, but some things I just can't find or come up with.
Adapter:
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;
}
MainFragment:
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);
}
});
change your code with below code
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
}
Now add click listener on the second button and do this
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 );
}
}
});
Now in your activity, wherever you are assigning OnItemClickListener
you need to override new methods you have created in the interface
So when you click on the second button then onSecondButtonClick()
will execute and the code under the overridden method will get 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
}
});