Firebase按时间范围查询[android]
我需要寻找与当前时间相匹配的主题。例如,当前时间为1:30。因此,如果我使用Firebase运行查询,它应该返回主题数学 这是我的JSON,其中“2012123458”是教员idFirebase按时间范围查询[android],android,time,firebase,Android,Time,Firebase,我需要寻找与当前时间相匹配的主题。例如,当前时间为1:30。因此,如果我使用Firebase运行查询,它应该返回主题数学 这是我的JSON,其中“2012123458”是教员id schedule:{ "2012123458" : { "Saturday" : { "-K8j0EkwmWjj3vMqB2d3" : { "classroom" : "Lab 3", "subject" : "Science", "time_end" : "2
schedule:{
"2012123458" : {
"Saturday" : {
"-K8j0EkwmWjj3vMqB2d3" : {
"classroom" : "Lab 3",
"subject" : "Science",
"time_end" : "2:00",
"time_start" : "0:00"
},
"asdasdasd" : {
"classroom" : "Lab 6",
"subject" : "PE",
"time_end" : "18:00",
"time_start" : "17:00"
},
"qweqweqweqwe" : {
"classroom" : "Lab 5",
"subject" : "PE",
"time_end" : "11:00",
"time_start" : "9:00"
}
}
}
}
我已尝试此操作,但返回NullPointerException
gridView.setAdapter(listFaculty);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FacultyList faculty = (FacultyList) parent.getItemAtPosition(position);
String faculty_id = faculty.getID();
//Display schedule in a Dialog box
final Dialog dialog = new Dialog(Faculty.this);
dialog.setContentView(R.layout.dialog_sched);
Firebase ref = new Firebase("https://infotrack.firebaseio.com/schedule/"+faculty_id+"/Saturday");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot: dataSnapshot.getChildren()){
schedule sched = postSnapshot.getValue(schedule.class);
TextView subject = (TextView) dialog.findViewById(R.id.txtSubject);
TextView time = (TextView) dialog.findViewById(R.id.txtTime);
TextView classroom = (TextView) dialog.findViewById(R.id.txtClassroom);
if(sched != null) {
if (checkTime(sched.getTime_start(), sched.getTime_end())){
Log.e("test", "x");
Log.e("test", "" + dataSnapshot.getChildrenCount());
Log.e("test", postSnapshot.getValue().toString());
Log.e("test", "0");
subject.setText(sched.getSubject());
Log.e("test", sched.getSubject());
time.setText(sched.getTime_start() + " - " + sched.getTime_end());
Log.e("test", "2");
classroom.setText(sched.getClassroom());
}
}
else {
Log.e("test", "null");
}
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Log.d("sched", firebaseError.getMessage());
}
});
dialog.show();
}
});
更新:
现在的问题是,它不适用于网格视图中的某些项。点击应用程序后,它会将我带回应用程序的主屏幕。一个空对话框出现一小段时间,然后返回主屏幕 哪一行抛出了
NullPointerException
?schedule
类是什么样子的?这里抛出异常:if(checkTime(sched.getTime\u start(),sched.getTime\u end())。我会用时间表更新我的帖子。。在这种情况下,将if(sched!=null)
放在它周围。很可能您正在收听的位置与您预期的不同,一个没有任何值的位置。例如,示例JSON的路径中没有Saturday
,但您正在设置一个包含该路径的路径的引用。当您侦听一个没有值的位置时,Firebase将调用带有空快照的onDataChange()
。我并不是在寻找您的完整应用程序。将问题还原为再现问题的最小代码+数据。有了这些,我们就可以知道发生了什么,而不用花时间在与问题无关的事情上。看见
public class schedule {
private String subject;
private String time_end;
private String time_start;
public schedule(){ }
public schedule(String subject, String time_end, String time_start){
this.subject = subject;
this.time_end = time_end;
this.time_start = time_start;
}
public String getSubject(){
return subject;
}
public String getTime_end(){
return time_end;
}
public String getTime_start(){
return time_start;
}
}