将值从 Activity 传递到适配器

Pro*_*emy 0 java android adapter android-arrayadapter android-intent

我有活动 A 和 B。活动 B 使用回收视图 Adpater 来显示信息列表。从活动 A 中,我将意图发送到用于工具栏标题的活动 B。但我在 Adapter 类中也需要相同的意图。

那么如何将 Intent 值从 Activity A 传递到 B,并在适配器类中也使用相同的 Intent。

我的活动 A 有代码

Intent new = new Intent(itemView.getContext(), B.class);
                phy.putExtra("key","This is title");   itemView.getContext().startActivity(new);
Run Code Online (Sandbox Code Playgroud)

在活动 B 中,我有

   getSupportActionBar().setTitle(getIntent().getStringExtra("key"));
Run Code Online (Sandbox Code Playgroud)

我也想要适配器类中的键值。

活动B的代码是

Intent new = new Intent(itemView.getContext(), B.class);
                phy.putExtra("key","This is title");   itemView.getContext().startActivity(new);
Run Code Online (Sandbox Code Playgroud)

我的适配器类有

   getSupportActionBar().setTitle(getIntent().getStringExtra("key"));
Run Code Online (Sandbox Code Playgroud)

提前致谢。

Bac*_* Vu 5

只需在适配器中再添加 1 个参数并传递标题

public RoomAdapter(ArrayList<Roompost> bookslist, String title){
        this.bookslist = bookslist;
        this.title = title;
}
Run Code Online (Sandbox Code Playgroud)

在你的活动 B 中onCreate

String title = getIntent().getStringExtra("key");
staggeredBooksAdapter = new RoomAdapter(list, title);
Run Code Online (Sandbox Code Playgroud)