Skip to content Skip to sidebar Skip to footer

Android Pass Value From Activity To Adapter

I want to pass variable from activity to adapter. My adapter looks like this public SampleAdapter(Activity context, ArrayList data){ this.context = context;

Solution 1:

Simply add the values to the constructor.

publicSimpleAdapter(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);

Solution 2:

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

privateString entity;

Catch it in Adapter Constructor

publicChatsAdapter(List<ChatModel> individualChats, String sendingEntity){

    this.individualChats = individualChats;
    entity = sendingEntity;

}

Use it anywhere in your adapter

Log.d(TAG, "entity: " +entity);

Post a Comment for "Android Pass Value From Activity To Adapter"