将 Activity 中的值传递给适配器
Pass value from Activity to adapter
我有 activity A 和 B。activity B 使用回收视图 Adpater 来显示信息列表。从 activity A,我将意图发送到 activity B,它用于工具栏标题。
但我也需要在 Adapter class 中有相同的意图。
那么我如何将意图值从 Activity A 传递到 B 并在适配器 class 中也使用相同的意图。
我的ActivityA有码
Intent new = new Intent(itemView.getContext(), B.class);
phy.putExtra("key","This is title"); itemView.getContext().startActivity(new);
在Activity B 中,我有
getSupportActionBar().setTitle(getIntent().getStringExtra("key"));
我也想要适配器 class 中的键值。
ActivityB的代码是
public class RoomlistActivity extends AppCompatActivity {
DatabaseReference dbreference,dbref;
RecyclerView rv;
ArrayList<Roompost> list;
private RoomAdapter staggeredBooksAdapter;
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_subject_books);
list = new ArrayList<>();
staggeredBooksAdapter = new RoomAdapter(list);
rv = (RecyclerView) findViewById(R.id.recyclerView);
rv.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);
rv.setAdapter(staggeredBooksAdapter);
}
我的适配器 class 有
public class RoomAdapter extends RecyclerView.Adapter<RoomAdapter.MyViewHolder> {
ArrayList<Roompost> bookslist;
CardView cv;
Roompost g;
public RoomAdapter(ArrayList<Roompost> bookslist){
this.bookslist = bookslist;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout,parent,false);
return new MyViewHolder(v);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView bookName,sellername,profile_details,update_info;
Button mSolved;
ImageView iv;
MyViewHolder(final View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.my_card_view);
update_info =(TextView) itemView.findViewById(R.id.update_info);
}
}
@Override
public int getItemCount() {
return bookslist.size();
}
}
提前致谢。
只需在您的适配器中再添加 1 个参数并传递标题
public RoomAdapter(ArrayList<Roompost> bookslist, String title){
this.bookslist = bookslist;
this.title = title;
}
而在你的 Activity B onCreate
String title = getIntent().getStringExtra("key");
staggeredBooksAdapter = new RoomAdapter(list, title);
您可以像这样通过构造函数调用将 Activity 中的值传递给适配器:
public class RoomlistActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
...
staggeredBooksAdapter = new RoomAdapter(list, getIntent().getStringExtra("key"));
...
}
...
}
你的适配器应该有这样的构造函数:
public class RoomAdapter extends RecyclerView.Adapter<RoomAdapter.MyViewHolder> {
ArrayList<Roompost> bookslist;
CardView cv;
Roompost g;
String key;
public RoomAdapter(ArrayList<Roompost> bookslist, String key){
this.bookslist = bookslist;
this.key = key;
}
...
}
``
我有 activity A 和 B。activity B 使用回收视图 Adpater 来显示信息列表。从 activity A,我将意图发送到 activity B,它用于工具栏标题。 但我也需要在 Adapter class 中有相同的意图。
那么我如何将意图值从 Activity A 传递到 B 并在适配器 class 中也使用相同的意图。
我的ActivityA有码
Intent new = new Intent(itemView.getContext(), B.class);
phy.putExtra("key","This is title"); itemView.getContext().startActivity(new);
在Activity B 中,我有
getSupportActionBar().setTitle(getIntent().getStringExtra("key"));
我也想要适配器 class 中的键值。
ActivityB的代码是
public class RoomlistActivity extends AppCompatActivity {
DatabaseReference dbreference,dbref;
RecyclerView rv;
ArrayList<Roompost> list;
private RoomAdapter staggeredBooksAdapter;
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_subject_books);
list = new ArrayList<>();
staggeredBooksAdapter = new RoomAdapter(list);
rv = (RecyclerView) findViewById(R.id.recyclerView);
rv.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);
rv.setAdapter(staggeredBooksAdapter);
}
我的适配器 class 有
public class RoomAdapter extends RecyclerView.Adapter<RoomAdapter.MyViewHolder> {
ArrayList<Roompost> bookslist;
CardView cv;
Roompost g;
public RoomAdapter(ArrayList<Roompost> bookslist){
this.bookslist = bookslist;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout,parent,false);
return new MyViewHolder(v);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView bookName,sellername,profile_details,update_info;
Button mSolved;
ImageView iv;
MyViewHolder(final View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.my_card_view);
update_info =(TextView) itemView.findViewById(R.id.update_info);
}
}
@Override
public int getItemCount() {
return bookslist.size();
}
}
提前致谢。
只需在您的适配器中再添加 1 个参数并传递标题
public RoomAdapter(ArrayList<Roompost> bookslist, String title){
this.bookslist = bookslist;
this.title = title;
}
而在你的 Activity B onCreate
String title = getIntent().getStringExtra("key");
staggeredBooksAdapter = new RoomAdapter(list, title);
您可以像这样通过构造函数调用将 Activity 中的值传递给适配器:
public class RoomlistActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
...
staggeredBooksAdapter = new RoomAdapter(list, getIntent().getStringExtra("key"));
...
}
...
}
你的适配器应该有这样的构造函数:
public class RoomAdapter extends RecyclerView.Adapter<RoomAdapter.MyViewHolder> {
ArrayList<Roompost> bookslist;
CardView cv;
Roompost g;
String key;
public RoomAdapter(ArrayList<Roompost> bookslist, String key){
this.bookslist = bookslist;
this.key = key;
}
...
}
``