Search code examples
androidfirebase-realtime-databaseandroid-recyclerviewandroid-adapterfirebaseui

Adding toast on FirebaseRecylerView Adapter specifically on "onBindViewHolder"


I have a problem displaying toast on an RecylerViewAdapter with firebase. I was gonna use it on an image button which is delete as you can see below the onBindViewHolder. But the problem is, getContext doesn't work

public class AddClassAdapter extends FirebaseRecyclerAdapter<AddClass, AddClassAdapter.myViewHolder>{ 

  /**
   * Initialize a {@link RecyclerView.Adapter} that listens to a Firebase query. See
   * {@link FirebaseRecyclerOptions} for configuration options.
   *
   * @param options
   */
  private OnItemClickListener listener, listener1;
  public AddClassAdapter(@NonNull FirebaseRecyclerOptions<AddClass> options) {
      super(options);
  }

  @Override
  protected void onBindViewHolder(@NonNull myViewHolder holder, int position, @NonNull AddClass model) {
      holder.className.setText(model.getClassName);  

      holder.deleteBtn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Toast.makeText(, "Course Added Successfully", Toast.LENGTH_SHORT).show();
          }
      });
  }

I've tried getContext() but it gives me an error, also, I tried using these in the main activity:

Context context = getApplicationContext();
AddClassAdapter adapter = new AddClassAdapater(context);

But it won't work and gives an error of

Required type:FirebaseRecyclerOptions <com.example.attendancechecker_application.AddClass>

Is there a way to have a toast in the RecyclerViewAdapter?


Solution

  • You instantiate the Adapter from your Activity class, there you have the Context Object - you can pass it in the Constructor of your Adapter Class and use it here for whatever you want.

    The error you get

    Required type:FirebaseRecyclerOptions <com.example.attendancechecker_application.AddClass>

    is because, you did not properly declare the constructor of your Adapter to accept the an Argument of Type FirebaseRecyclerOptions and an Argument of type Context.

    This should do the work:

    public class AddClassAdapter extends FirebaseRecyclerAdapter<AddClass, AddClassAdapter.myViewHolder>{ 
    
      private final Context context;
      private OnItemClickListener listener, listener1;
    
      public AddClassAdapter(@NonNull FirebaseRecyclerOptions<AddClass> options, Context context) {
          super(options);
          this.context = context;
      }
    
      @Override
      protected void onBindViewHolder(@NonNull myViewHolder holder, int position, @NonNull AddClass model) {
          holder.className.setText(model.getClassName);  
    
          holder.deleteBtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Toast.makeText(context, "Course Added Successfully", Toast.LENGTH_SHORT).show();
              }
          });
      }