Invoking an activity method from an adapter

Is it possible to call the method defined in the Activity from the ListAdapter ?

(I want to make Button in list's line and when this button is clicked, it should execute the method defined in the corresponding Activity. I tried to set onClickListener to my ListAdapter but I don’t know how to call this method, which way ...)

when I used Activity.this.method() I get the following error:

 No enclosing instance of the type Activity is accessible in scope 

Any idea?

+107
android list adapter
Aug 27 '12 at 12:45
source share
8 answers

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 (...) 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 your own class names in which you see your code, your activity, etc.

If you need to use the same adapter for several operations, follow these steps:

Create interface

 public interface IMethodCaller{ void yourDesiredMethod(); } 

Inject this interface into the actions required to invoke this method.

Then in Adapter getView () call:

 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 actions that do not require this call mechanism, the code will not be executed (if the verification failed).

+278
Aug 27 '12 at 13:00
source share

You can do it as follows:

Declare Interface:

 public interface MyInterface{ public void foo(); } 

Let your operation display this:

 public class MyActivity extends Activity implements MyInterface{ public void foo(){ //do stuff } } 

Then pass your activity to ListAdater:

 public MyAdapter extends BaseAdater{ private MyInterface listener; public MyAdapter(MyInterface listener){ this.listener = listener; } } 

And somewhere in the adapter, when you need to call this Activity method:

 listener.foo(); 
+112
Aug 27 2018-12-12T00:
source share

Original:

I understand the current answer, but I need a clearer example. Here is an example of what I used with Adapter (RecyclerView.Adapter) and Activity .

In your activity:

This will implement the interface that we have in our Adapter . In this example, it is called when the user clicks an item in the RecyclerView .

 public class MyActivity extends Activity implements AdapterCallback { private MyAdapter myAdapter; @Override public void onMethodCallback() { // do something } @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); myAdapter = new MyAdapter(this); } } 

In your adapter:

In Activity we initiated our Adapter and passed this as an argument to the constructor. This initiates our interface for our callback method. You can see that we use our callback method for user clicks.

 public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private AdapterCallback adapterCallback; public MyAdapter(Context context) { try { adapterCallback = ((AdapterCallback) context); } catch (ClassCastException e) { throw new ClassCastException("Activity must implement AdapterCallback.", e); } } @Override public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, int position) { // simple example, call interface here // not complete viewHolder.itemView.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { try { adapterCallback.onMethodCallback(); } catch (ClassCastException e) { // do something } } }); } public static interface AdapterCallback { void onMethodCallback(); } } 
+58
Dec 31 '15 at 5:54
source share

Basic and simple.

In your adapter, just use this.

((YourParentClass) context).functionToRun();

+12
Dec 28 '15 at 9:09
source share

Another way:

Write a method in your adapter, say, public void callBack () {}.

Now, when creating an object for the adapter in activity, override this method. The forwarding method will be called when the method is called in the adapter.

  Myadapter adapter = new Myadapter(){ @override public void callBack(){ // dosomething } }; 
+6
May 20 '15 at 9:21
source share

For Kotlin:

In your adapter just call

 (context as Your_Activity_Name).your_method_name() 
+1
Jul 01 '18 at 10:58
source share
 if(parent.getContext() instanceof yourActivity){ //execute code } 

this condition will allow you to fulfill something if for an Activity that has a GroupView requesting views from the getView() method of your adapter , there is yourActivity

NOTE: parent is a GroupView

0
May 26 '17 at 10:44
source share

MainActivity with invisible ADDTOCART button, MainActivity with Viewpager with fragments and fragments if any of the recyclerview from the fragment is clicked visible. Mainactivity addtocartbutton pleae provides some help.

0
Dec 09 '17 at 14:57
source share



All Articles