我想在切换时提供一个带有活动的arraylist,但有一个问题。getextras在切换活动后接收的对象为空。再加一点说明,当我在双循环器视图中单击内部循环视图时,我打算转到新的活动。
public class RoomAdapter extends RecyclerView.Adapter<RoomAdapter.CustomViewHolder> {
private Context context;
private ArrayList<RoomData> rooms;
public ArrayList<StudnetInRoomData> students = new ArrayList<>();
private LayoutInflater inflater;
private RoomData room;
View view;
public RoomAdapter(Context context, ArrayList<RoomData> rooms) {
this.context = context;
this.rooms = rooms;
this.inflater = LayoutInflater.from(context);
}
@Override
public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
view = inflater.inflate(R.layout.single_room, parent, false);
return new CustomViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull CustomViewHolder holder, int position) {
room = rooms.get(position);
holder.tvRoomNum.setText(String.valueOf(room.roomNum));
}
@Override
public int getItemCount() {
return rooms.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
public TextView tvRoomNum;
public CustomViewHolder(View itemView) {
super(itemView);
tvRoomNum = itemView.findViewById(R.id.tvRoomNumber);
tvRoomNum.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), PlusStudentActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("studentList", room.students);
intent.putExtras(bundle);
context.startActivity(intent);
}
});
}
}
}
在新活动中,调用getextras函数,如下所示。
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
ArrayList<StudnetInRoomData> students = (ArrayList< StudnetInRoomData>)bundle.getSerializable("studentList");
暂无答案!
目前还没有任何答案,快来回答吧!