Android pass value from activity to adapter

后端 未结 2 1722
广开言路
广开言路 2021-01-06 04:53

I want to pass variable from activity to adapter.

My adapter looks like this

  public SampleAdapter(Activity context, ArrayList da         


        
2条回答
  •  再見小時候
    2021-01-06 05:04

    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 individualChats, String sendingEntity) {
    
        this.individualChats = individualChats;
        entity = sendingEntity;
    
    }
    

    Use it anywhere in your adapter

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

提交回复
热议问题