1
推鍵這是我的數據看起來像:Android的 - 我們會根據孩子火力地堡
{
"courses":{
"Business":{
"PushKey_1":{
"courseC":"B1111",
"name":"Business Year 1"
},
"PushKey_2":{
"courseC":"B2222",
"name":"Business Year 2"
},
}
}
}
我試圖找回根據孩子推鍵。代碼我現在想:
String testCourseC = "B2222";
String showCourseKey;
DatabaseReference mRef = FirebaseDatabase.getInstance().getReference().child("courses").child("Business");
mRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()){
CourseDetails data = ds.getValue(CourseDetails.class);
String compareC = data.getCourseC();
if (compareC.equals(testCourseC)){
showCourseKey = dataSnapshot.getKey();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
textview1.setText(showCourseKey);
我CourseDetails
類:
public class CourseDetails {
private String courseC;
private String name;
public CourseDetails() {
}
public CourseDetails(String courseC, String name) {
this.courseC = courseC;
this.name = name;
}
public String getCourseC() {
return courseC;
}
public void setCourseC(String courseC) {
this.courseC = courseC;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
textview1應顯示PushKey_2。如果代碼錯誤,不應該textview1顯示「課程」?
我從上面的代碼中得到了NullPointerException。我已經閱讀了文檔並經歷了類似的問題,但仍然有錯誤。請不要介意我的愚蠢問題
當我試圖打印出從我的數據庫中的數據我才意識到,空白數據返回。我認爲這可能是我得到'NullPointerException'的原因。請參閱我的另一個問題[這裏](http://stackoverflow.com/questions/38814397/firebase-retrieve-data-in-android)。謝謝 –