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(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnasphot datas: 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
}
}
@Override
public void onCancelled(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(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(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
public void doNEXT(View view){
db = FirebaseDatabase.getInstance().getReference();
dbRef = db.child("Quiz Table");
dbRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String question = dataSnapshot.child(String.valueOf(X)).getValue(String.class);
String ansone = dataSnapshot.child(String.valueOf(X = X + 1)).getValue(String.class);
String anstwo = dataSnapshot.child(String.valueOf(X = X + 1)).getValue(String.class);
String ansthree = dataSnapshot.child(String.valueOf(X = X + 1)).getValue(String.class);
String ansfour = dataSnapshot.child(String.valueOf(X = X + 1)).getValue(String.class);
String corans = dataSnapshot.child(String.valueOf(X = X + 1)).getValue(String.class);
long valueX = 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;
String Z = String.valueOf(X);
etTableName.setText(Z);
if(X > valueX){
Intent intentN = new Intent(ViewDatabase.this,MainActivity.class);
startActivity(intentN);
}
}
@Override
public void onCancelled(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(new ValueEventListener() {
@Override
public void onDataChange(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);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}// END OF onCREATE BUNDLE
//=========================
And here is the onClick method of the button for scrolling
public void doONE(View view){
//btn onClick method
if (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?"