My adapter callback is not working.
I want to activate an alert dialog to pop up after the user clicks on one of the cards in recycle view. To implement the same I followed this answer. During runtime, when I click on any of the cards, my application crashes and refreshes back to the home page of my application.
This is the error output of the LogCat.
First half of Logcat.
Second half of Logcat:
My fragment file. This fragment is part of one of three fragments inside the tabbed activity.
SpecializationFrag.java
public class SpecializationFrag extends Fragment implements AdapterCallback{
// This is minimum reproducible code.
// other code which I found unnecessary are skipped
@Override
public void onMethodCallback( ) {
Toast.makeText(getContext(), "Inside fragment, methodcallback ", Toast.LENGTH_SHORT).show();
}
}
My adapter file.
SpecializationAdapter.java
public class SpecializationAdapter extends RecyclerView.Adapter<SpecializationAdapter.MyViewHolder> {
ArrayList<DoctorsList> doctorsListArrayList;
private AdapterCallback adapterCallback;
// some more code that I've skipped
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public SpecializationAdapter(Context context, ArrayList<DoctorsList> doctorsListArrayList){ // constructor for initialization
this.context = context;
this.doctorsListArrayList = doctorsListArrayList;
try{
adapterCallback = ((AdapterCallback) context);
}catch (ClassCastException e) {
Log.d("Error", e.toString());
}
}
@Override
public void onBindViewHolder(@NonNull com.example.appointmentmodule.Specialization.SpecializationAdapter.MyViewHolder holder, int position) {
holder.Specialization.setText(doctorsList.getSpecialization());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try{
adapterCallback.onMethodCallback();
}catch (ClassCastException e){
Log.d("Error", e.toString());
}
}
});
}
}
}
adapterCallback file.
AdapterCallback.java
public interface AdapterCallback {
void onMethodCallback();
}