我正在为俱乐部制作应用程序,应该记录会员出席情况。我在列表视图中无法正常使用它。
以下是数据库的一部分:
可能会有多项运动如空手道或某一天游泳。我希望会员在列表视图中出席。我会在一天之后尝试对其进行分区,但是现在我需要正确地获取数据。 这是我制作的模型:
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");
感谢任何帮助。
答案 0 :(得分:1)
我建议在HashMap
内创建另一个对象AttendedSport
作为变量而不是MemberAttendance
,而Firebase
将使用HashMap
映射数据。 } populateView
你可以得到像
memberAttendance.getAttendedSport().getSport()
和memberAttendance.getAttendedSport().getTimeStamp()
我不保证它会解决您的问题,但我认为它不易出错。
答案 1 :(得分:1)
您可能希望重构NoSQL数据库,如下所示:
member
---memberID
------attendance
---------attendance ID
------------attended
------------timestamp
------------date
例如:
member
---member1
------attendance
---------attendance1
------------soccer
------------14938372834
------------14-04-2017
---------attendance2
------------swimming
------------14938374323
------------14-04-2017
您不需要存储日期,因为use可以使用时间戳在Java中创建包含该信息的Date对象
Date date = new Date(timestamp);
使用此结构,您需要将POJO更改为:
public class MemberAttendance {
private String attended;
private long timestamp;
public MemberAttendance() {
}
public MemberAttendance(String attended, long timestamp) {
this.attended = attended;
this.timestamp = timestamp;
}
public String getAttended() {
return attended;
}
public void setAttended(String attended) {
this.attended = attended;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.attendedSport = attendedSport;
}
}
然后,您可以使用相同的Firebase查询,并在适配器中进行以下更改:
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) {
String sport = memberAttendace.getAttended();
Date date = new Date(memberAttendance.getTimestamp());
// Create views and assign values
TextView dayTxtView = (TextView) view.findViewById(R.id.dayTxtView);
dayTxtView.setText(new SimpleDateFormat("dd-MM-yyyy").format(date););
TextView sportTxtView = (TextView) view.findViewById(R.id.sportTxtView);
sportTxtView.setText(sport);
//I think you want to show the time here, not the date
TextView dateTxtView = (TextView) view.findViewById(R.id.dateTxtView);
dateTxtView.setText(new SimpleDateFormat("HH:mm:ss").format(date));
}
}
修改:关于按日期查询出席情况的评论,请查看我在上述结构中所做的修改,然后使用Firebase查询,例如
ref.child("members")
.child(<memberIdHere>)
.child("attendance")
.orderByChild("date")
.equalTo(<yourDateHere>);