Willy Laurents
Willy Laurents

Reputation: 25

Why cant I call a method from adapter?

I have an adapter class and an activity, I want to call the activity method from adapter

Adapter

holder.fab_plus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                        ((MainActivity)context).getTotalPrice();

                }
            });

MainActivity

@Override
public void getTotalPrice() {
    Toast.makeText(MainActivity.this, "Your Message Has Been Sent", Toast.LENGTH_SHORT).show();
}

I want to call getTotalPrice method form adapter

Upvotes: 1

Views: 2232

Answers (5)

Adarsh
Adarsh

Reputation: 310

You can send broadcast from Adapter and can receive broadcast in Main Activity.

Upvotes: 0

Nongthonbam Tonthoi
Nongthonbam Tonthoi

Reputation: 12953

Try this:

holder.fab_plus.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
          onClicked.getTotalPrice("Price");   
     }                
});

Your interface in adapter class:

 public interface OnClicked {
        void getTotalPrice(String price);
    }

At the top of your adapter class:

 private OnClicked onClicked;

and initialize it in adapter's constructor like:

public MyAdapter(Context context, ...) {
   onClicked = (OnClicked) context;
}

Now make your activity implement the interface like:

public class MyActivity extends Activity implements MyAdapter.OnClicked{
...

@Override
public void getTotalPrice(String price) {
    //do whatever you like here
    }
}

Now its done!

Upvotes: 3

Marius Kaunietis
Marius Kaunietis

Reputation: 704

You shouldn't call activity methods from adapter directly. There are several ways to do what you want. Two most popular ways are Interface and EventBus.

I recommend using EventBus, since it's much easier to implement, and requires less code.

In your Activity, create a method that reacts to specific event:

@Subscribe
public void onEvent(MyButtonClickEvent e){
    //do stuff
}

In your adapter, send such event:

EventBus.getDefault().post(new MyButtonClickEvent());

Don't forget to register for events in your activity:

onStart/onStop: EventBus.getDefault().register(this)/unregister(this);

For more information, visit EventBus github page: https://github.com/greenrobot/EventBus

Upvotes: 0

Sandeep Bhandari
Sandeep Bhandari

Reputation: 20379

If you are looking for how to write an interface here is what you can do :)

Declare interface in your adapter,

public class YourAdapter extends BaseAdapter {
    public interface ClickInformerInterface{
            public void getTotalPrice();
        }

Declare a property in YourAdapter to hold the reference to class which confirms the interface :)

ClickInformerInterface mInterface;

and in your onclick listener call this :)

holder.fab_plus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                        mInterface.getTotalPrice();

                }
            });

In your main activity :) after creating an adapter instance set this as adapter's interface :)

YourAdapter youradapter = new YourAdapter(....)
youradapter.mInterface = this;

Confirm ClickInformerInterface in your mainActivity using,

public class MainActivity extends Activity implements YourAdapter. ClickInformerInterface{

and finally implement method getTotalPrice() in MainActivity :)

public void getTotalPrice() {
    Toast.makeText(MainActivity.this, "Your Message Has Been Sent", Toast.LENGTH_SHORT).show();
}

Upvotes: 1

Ravi Vaghela
Ravi Vaghela

Reputation: 3420

You can use method directly in you adapter class. as like below.

Adapter

holder.fab_plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    getTotalPrice(context);

            }
        });

Method in adapter class means same class

 public void getTotalPrice(Context context) {
    Toast.makeText(context, "Your Message Has Been Sent", Toast.LENGTH_SHORT).show();
}

Upvotes: 0

Related Questions