6

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);
}
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
Evan Laksana
  • 410
  • 3
  • 7
  • 17
  • make a interface in the Adapter class , let's call buttonClickHandler interface which have onClickHandler(View v) method, now when u are init adapter inside fragment at the same time init this interface and call your doEdit method. – dex Apr 01 '16 at 03:35
  • duplicate of https://stackoverflow.com/questions/24502394/call-a-fragment-method-from-an-adapter – Rasika Sugathadasa Nov 07 '17 at 11:58

5 Answers5

12

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();
Abhinav Gupta
  • 2,225
  • 1
  • 14
  • 30
assif_tiger
  • 142
  • 1
  • 12
9

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...
  }
}
Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
huseyin
  • 1,367
  • 16
  • 19
2

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.

Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
0

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

0

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();
Akash Bisariya
  • 3,855
  • 2
  • 30
  • 42