Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android pass value from activity to adapter

Tags:

android

I want to pass variable from activity to adapter.

My adapter looks like this

  public SampleAdapter(Activity context, ArrayList<SampleBeans> data){
  this.context = context;
  this.data = data;
  }

My Activity looks like this

  newadapter = new SampleAdapter(this);
  newadapter.setId(Login_uuid_value);

Gives me error SampleAdapter cannot be applied to the activity.

like image 361
Unknown.geeko Avatar asked Dec 15 '22 02:12

Unknown.geeko


2 Answers

Simply add the values to the constructor.

public SimpleAdapter(Activity context, ArrayList<SimpleBeans> data, String mystring, int myInt){
  //use datas here
}

And use it like

myAdapter = new SimpleAdapter(this, data, myString, myInt);

Obiouvsly you can set all the datas you want, mine were some examples.

In your case you simply need to add the arrayList to the constructor.

myAdapter = new SimpleAdapter(this, myArrayList);
like image 69
Pier Giorgio Misley Avatar answered Dec 31 '22 07:12

Pier Giorgio Misley


Declare Your Adapter In Activity

private ChatsAdapter chatsAdapter;

Initiate Your Adapter in OnCreate in Activity (sendingEntity will be your String which you want to pass)

chatsAdapter = new ChatsAdapter(lIndividualChats, sendingEntity);

Declare Variable In Adapter

private String entity;

Catch it in Adapter Constructor

public ChatsAdapter(List<ChatModel> individualChats, String sendingEntity) {

    this.individualChats = individualChats;
    entity = sendingEntity;

}

Use it anywhere in your adapter

Log.d(TAG, "entity: " +entity);
like image 37
DragonFire Avatar answered Dec 31 '22 07:12

DragonFire