Mukesh Mishra
Mukesh Mishra

Reputation: 241

How to call a function of fragment from custom adapter

I have created a function in fragment. And I want to call that function when a button clicks which is in custom adapter but I am not able to call function of fragment from custom adapter.

My code for adapter is to click on a button and call fragment's function

                 viewHolder.accept.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                removeListItem(viewHolder.order_card_layout, position);

                android.support.v4.app.Fragment newFragment = new NewPageActivity();
                android.support.v4.app.FragmentTransaction ft = ((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction();

                 ft.sendorder(data.getorder_id());
                ft.add(R.id.framelay, newFragment).commit();


            }
        });

My function that is in fragment

    public class NewPageActivity extends Fragment{
        public void SendOrder( String OrderId)
{

    final String serverid = sp.getString("serverid", "null");
    Log.e("TAG", "SendOrder: "+serverid +OrderId );
    new SendOrder().execute(new Config().addNewOrder, serverid, OrderId);
}
      }

Upvotes: 4

Views: 15282

Answers (6)

Madan Gehlot
Madan Gehlot

Reputation: 99

Cast view context to your Parent Activity class in the custom adapter and then simply call your method. Steps: 1. Get view context in the custom adapter and cast to parent activity. 2. Cast context to Fragment class. 3. Use object to call fragment method.

Reference code -

  MainActivity hf = (MainActivity) view.getContext();
            FragmentManager manager = hf.getSupportFragmentManager();
            HomeFragment fragment = (HomeFragment) manager.findFragmentByTag("HomeFragment");
            fragment.loadJSON(true);

Don't instantiate Fragment again.

Upvotes: 0

Sagar Gangawane
Sagar Gangawane

Reputation: 1985

Calling the adapter in fragment.

  autoWitDrawRuleListAdapter = new AutoWithRuleListListAdapter(getContext(), AutoViewRuleFragment.this, lookupData);
                recycleRuleList.setAdapter(autoWitDrawRuleListAdapter);

Constructor:-

private Context context;
private Fragment fragment;
private ArrayList<ObjGetLookupDataResponseIn> lookupData;

 public AutoWithRuleListListAdapter(Context context, Fragment fragment, ArrayList<ObjGetLookupDataResponseIn> lookupData) {
    this.context = context;
    this.fragment = fragment;
    this.lookupData = lookupData;

}

@Override
public void onBindViewHolder(final ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
    String output = lookupData.get(position).getDisplayName().substring(0, 1).toUpperCase() + lookupData.get(position).getDisplayName().substring(1);
    holder.radioBen.setText(output);
    holder.radioBen.setOnCheckedChangeListener(null);
    holder.radioBen.setChecked(lookupData.get(position).isSelected());

    holder.radioBen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            resetAll();
            lookupData.get(position).setSelected(true);
            notifyDataSetChanged();
            ((AutoViewRuleFragment)fragment).ruleName(position);
        }
    });


}

Method in the Fragment class.

  public void ruleName(int position) {
    //your logic.
}

Hope this help.Vote if you find it useful.Happy coding.

Upvotes: 6

Sneha Sarkar
Sneha Sarkar

Reputation: 731

In your code to call the function of Fragment you have created a object of current Fragment using new.

android.support.v4.app.Fragment newFragment = new NewPageActivity();

It is not a current way to call a function from fragment to adapter.

Suppose, you have a list view in which you want to set values from your adapter. In that case, put this code is your Fragment:

SelectUserAdapter adapter = new SelectUserAdapter(selectUserModels, getActivity(), CurrentFragment.this);
        listView.setAdapter(adapter);

In your Custom Adapter put this code:

public class SelectUserAdapter extends BaseAdapter {

    private List<SelectUserModel> contactList;
    private Context context;
    private Fragment fragment;

    public SelectUserAdapter(List<SelectUserModel> selectUserModels, Context context, Fragment fragment) {
        contactList = selectUserModels;
        this.context = context;
        this.fragment = fragment;
    }

Then to call the method of Fragment use this code in adapter:

((CurrentFragment) fragment).sendorder(data.getorder_id());

Upvotes: 0

Rishabh Mahatha
Rishabh Mahatha

Reputation: 1271

pass fragment in your adapter from fragment like

UserAdapter adapter = new UserAdapter(selectUserModels, getActivity(), FragmentName.this);

and the use that fragment in your adapter like:

 private Context context;
            private Fragment fragment;

     public UserAdapter(List<UserModel> selectUserModels, Context context, Fragment fragment) {
            contactList = selectUserModels;
            this.context = context;
            this.fragment = fragment;
        }

then call the method like

((FragmentName) fragment).fragmentMethod();

Upvotes: 2

sohan shetty
sohan shetty

Reputation: 310

Create an Interface Class in adapter and implement in your fragment class like below

Inside Adapter class

         public Interface CallBack{
         void yourMethodName();

         }

Now in fragment class implement your interface method like below

public class YourFragment implements CallBack{
     ...........
     @Override
     public void yourMethodName(){
      //"here call your fragment method or do any bussiness logic 
      }    
  }

Finally you should call your interface method in your adapter onclick listener like below before that pass your interface instance to your adapter constructor

public class YourAdapterClass extends BaseAdapter {
  private CallBack mCallBack;
   public YourAdapterClass (CallBack callback){
    mCallBack = callback;
      }
    }

Then inside your onClickListener call your interface method like this

    @Override
        public void onClick(View v) {
           mCallBack.yourMethodName();


        }
    });

Done

Upvotes: 18

jeel raja
jeel raja

Reputation: 667

You can make your function static and call that function with fragment class name in your adapter.

In your fragment do like this,

public static void SendOrder(String OrderId)
{

    final String serverid = sp.getString("serverid", "null");
    Log.e("TAG", "SendOrder: "+serverid +OrderId );
    new SendOrder().execute(new Config().addNewOrder, serverid, OrderId);
}

inside you fragment,

viewHolder.accept.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                removeListItem(viewHolder.order_card_layout, position);

                 fragmentClassName.Sendorder(data.getorder_id());

            }
        });

Upvotes: 1

Related Questions