我希望在Users内部检索作为ArrayList字段的元素,并在recyclerview中显示每个计划对象及其字段。
我使用了大多数人使用的模型,适配器模式,但是我不知道如何为“计划”检索数据,因为它是嵌套的。
我收到一条错误消息,说回收者认为没有adpater连接。
我已尝试这样做
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragment_schedule,container,false);
final ArrayList<ScheduleItem> schedulelist = new ArrayList<>();
firebaseDatabase=firebaseDatabase.getInstance();
myRef=firebaseDatabase.getReference("Users").child(userID);
recyclerView= view.findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
mLayoutman= new LinearLayoutManager(getActivity());
madapter= new ScheduleAdapter(schedulelist);
FirebaseUser user = firebaseAuth.getCurrentUser();
userID = user.getUid();
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
User user = new User();
user = ds.getValue(User.class);
ArrayList<ScheduleItem> scheduleItems = user.getSchedule();
for (int i = 0; i < scheduleItems.size(); i++) {
Parent = scheduleItems.get(i).getParent_N();
Kid = scheduleItems.get(i).getKid_N();
Time = scheduleItems.get(i).getTime();
Date= scheduleItems.get(i).getDate();
Place= scheduleItems.get(i).getPlace();
schedulelist.add(new ScheduleItem(Parent, Kid, Time,Date,Place));
Log.d(TAG, "onDataChange: ");
}
recyclerView.setAdapter(madapter);
recyclerView.setLayoutManager(mLayoutman);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
System.out.print("Failed");
}
});
return view;
}
}
这是我的适配器
//formatted code
public class ScheduleAdapter extends RecyclerView.Adapter<ScheduleAdapter.ScheduleViewHolder> {
private ArrayList<ScheduleItem> displaylist;
public static class ScheduleViewHolder extends RecyclerView.ViewHolder{
private TextView Parent_name;
private TextView Kid_name;
private TextView Time;
private TextView Date;
private TextView Place;
public ScheduleViewHolder(@NonNull View itemView) {
super(itemView);
Parent_name = itemView.findViewById(R.id.s_parent);
Kid_name=itemView.findViewById(R.id.s_kid);
Time=itemView.findViewById(R.id.s_time);
Date=itemView.findViewById(R.id.s_date);
Place=itemView.findViewById(R.id.s_place);
}
}
public ScheduleAdapter(ArrayList<ScheduleItem> list) {
this.displaylist=list;
}
@NonNull
@Override
public ScheduleViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.schedule_item,viewGroup,false);
ScheduleViewHolder x= new ScheduleViewHolder(view);
return x;
}
@Override
public void onBindViewHolder(@NonNull ScheduleViewHolder scheduleViewHolder, int i) {
ScheduleItem current=displaylist.get(i);
scheduleViewHolder.Parent_name.setText(current.getParent_N());
scheduleViewHolder.Kid_name.setText(current.getKid_N());
scheduleViewHolder.Time.setText(current.getTime());
scheduleViewHolder.Date.setText(current.getDate());
scheduleViewHolder.Place.setText(current.getPlace());
}
@Override
public int getItemCount() {
return displaylist.size();
}
}
这是我的时间表模型`
public class ScheduleItem {
private String Parent_N;
private String Kid_N;
private String Time;
private String Date;
private String Place;
public ScheduleItem(String parent_N, String kid_N, String time, String date, String place) {
Parent_N = parent_N;
Kid_N = kid_N;
Time = time;
Date = date;
Place = place;
}
public String getParent_N() {
return Parent_N;
}
public void setParent_N(String parent_N) {
Parent_N = parent_N;
}
public String getKid_N() {
return Kid_N;
}
public void setKid_N(String kid_N) {
Kid_N = kid_N;
}
public String getTime() {
return Time;
}
public void setTime(String time) {
Time = time;
}
public String getDate() {
return Date;
}
public void setDate(String date) {
Date = date;
}
public String getPlace() {
return Place;
}
public void setPlace(String place) {
Place = place;
}
}
` 和我的用户模型
public class User {
private User userObject;
public String email, username, userType;
public ArrayList<ScheduleItem> schedule;
public User(){
}
public User(String email){
this.email = email;
}
public User(String email, String username, String userType) {
this.email = email;
this.username = username;
this.userType = userType;
}
public User(String email, String username, String userType, ArrayList<ScheduleItem> scheduleIN) {
this.email = email;
this.username = username;
this.userType = userType;
this.schedule=scheduleIN;
}
public String getEmail() {
return email;
}
public String getUsername() {
return username;
}
public void setEmail(String email) {
this.email = email;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserType() {
return userType;
}
public void setUserType(String userType) {
this.userType = userType;
}
public ArrayList<ScheduleItem> getSchedule() {
return schedule;
}
public void setSchedule(ArrayList<ScheduleItem> scheduleIN) {
this.schedule = scheduleIN;
}
}
答案 0 :(得分:0)
“我收到一条错误消息,说回收者认为没有adpater附件。”
recyclerView.setAdapter(madapter);