Android 无法从 Firebase 获取列表大小
Cannot get list size from Firebase on Android
我的日志可以打印所有 Exercise
对象。但是 listExercise
当我打电话时是空的。有什么问题吗?
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Exercise exercise = postSnapshot.getValue(Exercise.class);
String temp = exercise.toString();
Log.d("exercise: ", temp + "\n"); // can log all
listExercises.add(exercise);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Log.d("The read failed: ", firebaseError.getMessage());
}
});
Log.d("LISTSIZE: ", String.valueOf(listExercises.size())); // return 0
[更新]任何人需要的答案:
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Exercise exercise = postSnapshot.getValue(Exercise.class);
listExercises.add(exercise);
}
// action with data here
Exercise ex = listExercises.get(0);
tvQuestion.setText(ex.getQuestion);
}
当方法 "paintListInScreen" 被调用时,你肯定知道列表有一些内容(或者可能没有,这取决于服务器,但你要与线程保持一致)。
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Exercise exercise = postSnapshot.getValue(Exercise.class);
String temp = exercise.toString();
Log.d("exercise: ", temp + "\n"); // can log all
listExercises.add(exercise);
}
Log.d("LISTSIZE: ", String.valueOf(listExercises.size())); // return 0
paintListInScreen(listExercises);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Log.d("The read failed: ", firebaseError.getMessage());
}
});
对于 Java 8 和更新版本(对于 Android API 需要 24 级):
int size = dataSnapshot.getChildren().spliterator().getExactSizeIfKnown() ;
旧版本:
int size = 0
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
size++ ;
}
获取datasnapshot对象的大小:
dataSnapshot.getChildrenCount();
我的日志可以打印所有 Exercise
对象。但是 listExercise
当我打电话时是空的。有什么问题吗?
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Exercise exercise = postSnapshot.getValue(Exercise.class);
String temp = exercise.toString();
Log.d("exercise: ", temp + "\n"); // can log all
listExercises.add(exercise);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Log.d("The read failed: ", firebaseError.getMessage());
}
});
Log.d("LISTSIZE: ", String.valueOf(listExercises.size())); // return 0
[更新]任何人需要的答案:
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Exercise exercise = postSnapshot.getValue(Exercise.class);
listExercises.add(exercise);
}
// action with data here
Exercise ex = listExercises.get(0);
tvQuestion.setText(ex.getQuestion);
}
当方法 "paintListInScreen" 被调用时,你肯定知道列表有一些内容(或者可能没有,这取决于服务器,但你要与线程保持一致)。
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Exercise exercise = postSnapshot.getValue(Exercise.class);
String temp = exercise.toString();
Log.d("exercise: ", temp + "\n"); // can log all
listExercises.add(exercise);
}
Log.d("LISTSIZE: ", String.valueOf(listExercises.size())); // return 0
paintListInScreen(listExercises);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Log.d("The read failed: ", firebaseError.getMessage());
}
});
对于 Java 8 和更新版本(对于 Android API 需要 24 级):
int size = dataSnapshot.getChildren().spliterator().getExactSizeIfKnown() ;
旧版本:
int size = 0
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
size++ ;
}
获取datasnapshot对象的大小:
dataSnapshot.getChildrenCount();