Evan Laksana
Evan Laksana

Reputation: 443

How to call method in fragment from adapter Android

I need help so I have a fragment which has a RecycleView and inside the RecycleView there is a button.

The button after click must open the dialog which already declared in base fragment so I only call like openDialog(DIALOG_CHECK);

Now how can I call that dialog on my adapter I already make a method in fragment and call it from the adapter and make an error "Java lang null pointer"

This is my code :

DeliveryFragment delivFrag = new DeliveryFragment();
holder.editButton.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         delivFrag.doEdit();
     }
});

And in fragment

public void doEdit(){
   openDialog(DIALOG_EDIT_ITEM);
}

Upvotes: 6

Views: 32316

Answers (5)

Rohit Singh
Rohit Singh

Reputation: 18202

Create a callback with an interface

It is one of the most used patterns in Software Engineering.

A -> B

A and B represent the entities and -> represents the direction of communication

In our case, it can be represented as

Adapter -> Fragment

To communicate between the entities we need some kind of bridge. In these patterns, an interface serves as a bridge.

Step 1: Create an interface

interface FragmentCallback{

    public void doSomething(){

    }

}

Step 2: Make your Fragment implements the interface and override the method

class YourFragment implements FragmentCallback{

       // Your fragment code

     @Override
     public void doSomething(){
        Log.d(TAG, "Clicked")
     }

}

Step 3: Initiate the Callback in Adapter

class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

      public FragmentCallback callback;

      public MyAdapter(FragmentCallback callback){
            thid.call = callback;     //<--This is how you initialise it     
      }

      callback.doSomething(). //<-- This is how you call callback method

}

##How to create an instance of Adapter in Fragment?

MyAdapter adapter = new MyAdapter(this)

Sample code: You can find a sample code here.
Disclaimer: The sample code is based on the above idea but it is not very easy to understand.

Upvotes: 2

assif_tiger
assif_tiger

Reputation: 142

Update your adapter constructor to accept the Fragment as a parameter.

customAdapter = new CustomAdapter(myContext, android.R.layout.simple_list_item_1, getList, HomeFragment.this);

Adapter Class:

public CustomAdapter(Context context, int id, HomeFragment fragment) {
    this.fragment = fragment;
}

Call in Adapter :

((FragmentName)fragment).methodName();

Upvotes: 12

huseyin
huseyin

Reputation: 1427

Just a simple example for better understanding. Use an interface.

public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> {    
  private static OnItemClickListener mOnItemClickLister;

  public interface OnItemClickListener {
    void onItemClicked(View view, int pos);
  }

  public void setOnItemClickListener(OnItemClickListener listener) {    
    mOnItemClickLister = listener;
  }

  public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {    
    Button mBtnTest;
    Context mContext;

    //We also create a constructor that accepts the entire item row
    //and does the view lookups to find each subview.
    public ViewHolder(Context context, View itemView) {    
      //Stores the itemView in a public final member variable that can be used
      //to access the context from any ViewHolder Instance
      super(itemView);    
      mContext = context;
      mBtnTest = (Button) itemView.findViewById(R.id.message_button);
      itemView.setOnClickListener(this);
    }

    @Override public void onClick(View v) {
      int position = v.getLayoutDirection();
      mOnItemClickLister.onItemClicked(v, position);
    }
  }
}   

Fragment Part

class FragmentTest extends Fragment implements OnItemClickListener {    
  TestAdapter adapter = new TestAdapter(); //you can initialize according to  your logic

  //set the fragment as a listener to adapter
  this.adapter.setOnItemClickListener(onItemClickListener);

  public void onItemClicked(View view, int pos) {
    //do whatever you want here...
  }
}

Upvotes: 9

Akash Bisariya
Akash Bisariya

Reputation: 4754

You can send Fragment Instance in to constructor of your Adapter and then you can use this instance to call the method in that fragment.

public MyCartRecycleAdapter(Context context, List<CartData> list, MyFragmentNew myFragmentNew) {
    this.list = list;
    this.mContext = (Activity) context;
    this.myFragmentNew = myFragmentNew;
}

and then

myFragmentNew.MethodName();

Upvotes: 0

Satya siva prasad
Satya siva prasad

Reputation: 495

You have to write an interface in your Adapter class and implement that functionality in your fragment from where your calling your adapter.

Here is sample app for recycler view with item click action. Check once it may useful to you. https://www.dropbox.com/s/2q1ywnehz454axw/SamplePro_Recycler.zip?dl=0

Upvotes: 0

Related Questions