Call Activity Method from Adapter

Call Activity method from adapter

Yes you can.

In the adapter Add a new Field :

private Context mContext;

In the adapter Constructor add the following code :

public AdapterName(......, Context context) {
//your code.
this.mContext = context;
}

In the getView(...) of Adapter:

Button btn = (Button) convertView.findViewById(yourButtonId);
btn.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
if (mContext instanceof YourActivityName) {
((YourActivityName)mContext).yourDesiredMethod();
}
}
});

replace with your own class names where you see your code, your activity etc.

If you need to use this same adapter for more than one activity then :

Create an Interface

public interface IMethodCaller {
void yourDesiredMethod();
}

Implement this interface in activities you require to have this method calling functionality.

Then in Adapter getView(), call like:

Button btn = (Button) convertView.findViewById(yourButtonId);
btn.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
if (mContext instanceof IMethodCaller) {
((IMethodCaller) mContext).yourDesiredMethod();
}
}
});

You are done. If you need to use this adapter for activities which does not require this calling mechanism, the code will not execute (If check fails).

Call activity method from recyclerview adapter

Try This one

public MethodCaller methodCaller;
public AddressAdapter(Context context, List<Address> list,MethodCaller methodCaller) {
this.context = context;
this.list = list;
this.methodCaller = methodCallerl;

}

@Override
public void onBindViewHolder(@NonNull AddressAdapter.ViewHolder holder, final int position) {
Address address = list.get(position);

holder.btn_deleteAddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Address address = list.get(position);
final String id = address.getId();
methodCaller .deleteAddress(id);
}
});
}

AddressActivity.java

pass interface in your adapter like this;

new AddressAdapter(context,list,this);

How to call main activity method in Recyclerview adapter class

Pass your Activity's reference to Adapter in Constructor or set it by setter method and call your TestMe method.

if(activity instanceof MainActivity){
((MainActivity)activity).TestMe();
}

How to call an Activity method in an Adapter

Pass function in your adapter

class YourAdapter(private val listener : (Double, Double,String) -> Unit) : RecyclerView.Adapter<RecyclerView.ViewHolder>()

and in your viewHoler

val latitude = user?.location?.location?.latitude?.toDouble()
val longitude = user?.location?.location?.longitude?.toDouble()
val name : String = "Localisation de: " + user?.name?.nom + " " + user?.name?.prenom


holder.buttonMap.setOnClickListener {
listener(latitude,longitude,name)
}

And in your MainActivity where you set the adapter

YourAdapter(){latitude: Double, longitude: Double, name: String -> launchMapActivity(latitude,longitude,name)}

Call an Activity method from inside adapter?

I Recommending on using interface as listener.

For example:

1, create your interface:

public interface MyListener {

void folderClicked();
}

2, Let your ActivityA to implement this Interface like this:

public class ActivityA extends Activity implements MyListener 

3, you will have to auto overide the method folderClicked it will look like this:

@Override
protected void folderClicked() {
// Do your stuff here.

}

4, send the activity listener to the adapter with constructor like this:

MyAdpater adapter = new MyAdpater(ActivityA.this);

5, in your adapter class your code should be like this :

public class TimeLineAdapter extends BaseAdapter {

private MyListener mListener;


public TimeLineAdapter(MyListener listener) {
super();

mListener = listener;


}

holder.iconImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onFolderClicked()
//code to do stuff when the image is clicked
}


Related Topics



Leave a reply



Submit