我需要从“出勤”节点填充列表视图:
每个列表项应显示运动,日期和时间。以下是我正在做的事情:
final DatabaseReference attendanceRef = database.getReference()
.child(Constants.MEMBERS_NODE).child(userID).child("attendance").child(today);
attendanceRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
String sport = snapshot.getKey();
String date = (String) snapshot.child("date").getValue();
String timeStamp = (String) snapshot.child("timestamp").getValue();
}
}
请注意,我不能只获取快照的值,因为我需要获取密钥,这是运动名称。我不确定如何处理它。感谢您的任何建议
答案 0 :(得分:0)
创建一个hashmap来映射值。
HashMap<String,String> sportsMap = snapshot.getValue();
String date = sportsMap.get("date");
答案 1 :(得分:0)
我希望列表项显示体育名称及其参加的时间和日期。答案并不复杂,但需要深入了解。
我应该创建一个模型类来获取日期和时间戳:
public class MemberAttendance {
private String sport;
private long timestamp;
public MemberAttendance() {
}
public MemberAttendance(String sport, long timestamp) {
this.sport = sport;
this.timestamp = timestamp;
}
public String getSport() {
return sport;
}
public long getTimestamp() {
return timestamp;
}
public void setSport(String sport) {
this.sport = sport;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
}
然后像以前一样创建一个引用:
final DatabaseReference attendanceRef = database.getReference()
.child(Constants.MEMBERS_NODE).child(userID).child("attendance").child(today);
然后像这样初始化适配器:
attendanceAdapter = new MemberAttendanceAdapter(getActivity(), MemberAttendance.class,
R.layout.list_item_attendance, attendanceRef);
attendanceListView.setAdapter(attendanceAdapter);
真正的诀窍在于我需要让运动等于参考键,这是运动名称:
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 = getRef(i).getKey();
// Create views and assign values
TextView sportTxtView = (TextView) view.findViewById(R.id.sportTxtView);
sportTxtView.setText(sport);
// This time stamp can return date and time, so there is no need to create a date getter
Date timestamp = new Date(memberAttendance.getTimestamp());
TextView dayTxtView = (TextView) view.findViewById(R.id.dayTxtView);
dayTxtView.setText(new SimpleDateFormat("dd-MM-yyyy").format(timestamp));
// Show the time
TextView dateTxtView = (TextView) view.findViewById(R.id.dateTxtView);
dateTxtView.setText(new SimpleDateFormat("hh:mm a").format(timestamp));
}
}