簡體   English   中英

Android將值從活動傳遞到適配器

[英]Android pass value from activity to adapter

我想將變量從活動傳遞給適配器。

我的適配器看起來像這樣

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

我的活動看起來像這樣

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

給我錯誤SampleAdapter無法應用於活動。

只需將值添加到構造函數中。

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

並使用它

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

您可以設置所需的所有數據,我的一些例子。

在您的情況下,您只需要將arrayList添加到構造函數中。

myAdapter = new SimpleAdapter(this, myArrayList);

在活動中聲明您的適配器

private ChatsAdapter chatsAdapter;

在Activity中的OnCreate中啟動適配器(sendingEntity將是您要傳遞的String)

chatsAdapter = new ChatsAdapter(lIndividualChats, sendingEntity);

在適配器中聲明變量

private String entity;

在Adapter Constructor中捕獲它

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

    this.individualChats = individualChats;
    entity = sendingEntity;

}

在適配器的任何位置使用它

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM