You've already found how to limit the amount of data return by Firebase: .limitToFirst(1)
.
Not to get the next item in Firebase, you need to know at which item to start returning data. In your case that means you need to:
- Know the key of the node that you're currently showing.
- Retrieve 2 nodes starting at that key.
Given that you already key lastKey
, you can read the next result with:
private void read() {
Query query = mDatabase.child("question").child("cf").orderByKey();
if (lastKey != null) {
query = query.startAt(lastKey).limitToFirst(2);
}
else {
query = query.limitToFirst(1);
}
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot recipeSnapshot: dataSnapshot.getChildren()) {
lastKey = recipeSnapshot.getKey();
String pergunta = Objects.requireNonNull(recipeSnapshot.child("question").getValue()).toString();
Toast.makeText(MainActivity.this, pergunta, Toast.LENGTH_SHORT).show();
}
}
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…