我正在爲會員出席應記錄的俱樂部開發應用程序。我無法在列表視圖中正確獲取它。正確獲取Firebase數據
這裏是數據庫的一部分:
可能有不止一個運動參加像空手道或游泳某一天。我想在列表視圖中獲取該成員的出席人數。我會盡量在一天之後對其進行分區,但現在我需要正確地獲取數據。 這裏是我做出的模型:
public class MemberAttendance {
private String day;
private HashMap<String, Object> attendedSport;
public MemberAttendance() {
}
public MemberAttendance(String day, HashMap<String, Object> attendedSport) {
this.day = day;
this.attendedSport = attendedSport;
}
public String getDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
public HashMap<String, Object> getAttendedSport() {
return attendedSport;
}
public void setAttendedSport(HashMap<String, Object> attendedSport) {
this.attendedSport = attendedSport;
}
}
這裏是適配器:
public class MemberAttendanceAdapter extends FirebaseListAdapter<MemberAttendance> {
public MemberAttendanceAdapter(Activity activity, Class<MemberAttendance> modelClass, int modelLayout, Query ref) {
super(activity, modelClass, modelLayout, ref);
}
@Override
protected void populateView(View view, MemberAttendance memberAttendance, int i) {
// Get values from firebase database
String day = memberAttendance.getDay();
HashMap<String, Object> hashMap = memberAttendance.getAttendedSport();
String sport = (String) hashMap.get("attended");
String date = (String) hashMap.get("timestamp");
// Create views and assign values
TextView dayTxtView = (TextView) view.findViewById(R.id.dayTxtView);
dayTxtView.setText(day);
TextView sportTxtView = (TextView) view.findViewById(R.id.sportTxtView);
sportTxtView.setText(sport);
TextView dateTxtView = (TextView) view.findViewById(R.id.dateTxtView);
dateTxtView.setText(date);
}
}
在這裏,我怎麼嘗試設置適配器:
final FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference attendanceRef = database.getReference()
.child(Constants.MEMBERS_NODE).child(userID).child("attendance");
attendanceAdapter = new MemberAttendanceAdapter(getActivity(), MemberAttendance.class,
R.layout.attendance_list_item, attendanceRef);
attendanceListView.setAdapter(attendanceAdapter);
到目前爲止,沒有工作,並就使用HashMap.get(..)對象引用空指針異常
String sport = (String) hashMap.get("attended");
任何幫助表示讚賞。
的getAttendedSport HashMap試圖獲得一個名爲「attendSport」的節點,但沒有名爲「attendSport」的節點,而只是看起來像一個隨機ID。 – Linxy