Firebase For Android Studio - How Do You Loop Through Each Item In A Database?
Solution 1:
To iterate in the above database, try this:-
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Categories");
ref.addValueEventListener(newValueEventListener(){
@OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
for(DataSnasphotdatas: dataSnasphot.getChildren()){
String num=datas.child("1").getValue().toString();
String twos=datas.child("2").getValue().toString();
String threes=datas.child("3").getValue().toString();
String four=datas.child("4").getValue().toString();
//so on
}
}
@OverridepublicvoidonCancelled(FirebaseError firebaseError) {
}
});
Using the for loop, then you will be able to iterate inside Barbs
and Tetras
, and get the values of the attributes insides them(values of 1,2,3,4,5,6..)
for a button:
btn.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View v) {
Intentintent=newIntent(getBaseContext(), FishInfo.class);
intent.putExtra("typeName", num);
intent.putExtra("typeInfo", twos);
startActivity(intent);
finish();
}
});
Solution 2:
We have a very flat firebase data structure and to loop through the DB with a button click becomes intuitive once you understand the structure is KEY String "1" and VALUE is a String. This solution is less than elegant but it works. The issue to consider when adding data you will need a way to increment you KEY String. If you get the dataSnapshot.getChildrenCount() and realize you have 6 rows per record then with 2 records you have a count of 12 so your 3rd record starts at 12 + 1 Enjoy the code
publicvoiddoNEXT(View view){
db = FirebaseDatabase.getInstance().getReference();
dbRef = db.child("Quiz Table");
dbRef.addValueEventListener(newValueEventListener() {
@OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
Stringquestion= dataSnapshot.child(String.valueOf(X)).getValue(String.class);
Stringansone= dataSnapshot.child(String.valueOf(X = X + 1)).getValue(String.class);
Stringanstwo= dataSnapshot.child(String.valueOf(X = X + 1)).getValue(String.class);
Stringansthree= dataSnapshot.child(String.valueOf(X = X + 1)).getValue(String.class);
Stringansfour= dataSnapshot.child(String.valueOf(X = X + 1)).getValue(String.class);
Stringcorans= dataSnapshot.child(String.valueOf(X = X + 1)).getValue(String.class);
longvalueX= dataSnapshot.getChildrenCount();
System.out.println("################################################ COUNT Children "+valueX);
etQuestion.setText(question);
etAnsOne.setText(ansone);
etAnsTwo.setText(anstwo);
etAnsThree.setText(ansthree);
etAnsFour.setText(ansfour);
etCorAns.setText(corans);
X = X + 1;
StringZ= String.valueOf(X);
etTableName.setText(Z);
if(X > valueX){
IntentintentN=newIntent(ViewDatabase.this,MainActivity.class);
startActivity(intentN);
}
}
@OverridepublicvoidonCancelled(DatabaseError databaseError) {
}
});
}
Solution 3:
As Grendel stated this is not "elegant" but it works! To improve on his answer we all know how to use ArrayList so here is another version where the ArrayList is populated (loaded) in the onCreate method and with a button click outside the onCreate the use can scroll through the objArrayList be sure to declare the objArrayList so it is Global Place it above the onCreate like this ArrayList objArrayList = new ArrayList<>()
Here is the onCreate code
db = FirebaseDatabase.getInstance().getReference();
dbRef = db.child("Quiz Table");
dbRef.addValueEventListener(newValueEventListener() {
@OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
long valueX = dataSnapshot.getChildrenCount();
for (DataSnapshot child : dataSnapshot.getChildren())
objArrayList.add(child.getValue(String.class));
String S = String.valueOf(objArrayList.size());
System.out.println("$$$$$$$$$$$$$$$$$$$$$$ objArrayList SIZE " + S);
}
@OverridepublicvoidonCancelled(DatabaseError databaseError) {
}
});
}// END OF onCREATE BUNDLE//=========================
And here is the onClick method of the button for scrolling
publicvoiddoONE(View view){
//btn onClick methodif (objArrayList != null) {
String question = objArrayList.get(Z);
String ansone = objArrayList.get(Z = Z +1);
String anstwo = objArrayList.get(Z = Z +1);
String ansthree = objArrayList.get(Z = Z +1);
String ansfour = objArrayList.get(Z = Z +1);
String corans = objArrayList.get(Z = Z +1);
etQuestion.setText(question);
etAnsOne.setText(ansone);
etAnsTwo.setText(anstwo);
etAnsThree.setText(ansthree);
etAnsFour.setText(ansfour);
etCorAns.setText(corans);
}
Z = Z + 1;
if(Z > dataSnapshot.getChildrenCount()){
Intent intentN = new Intent(ViewDatabase.this,MainActivity.class);
startActivity(intentN);
}
}
Post a Comment for "Firebase For Android Studio - How Do You Loop Through Each Item In A Database?"