Firebase For Android Studio - How do you loop through each item in a database?

≡放荡痞女 提交于 2019-12-24 23:12:49

问题


I have a firebase real-time database that looks like this:


I want to create a button in this format:


How do I do this?

Each Button should also do this:

Intent intent = new Intent(getBaseContext(), FishInfo.class);
            intent.putExtra("typeName", "Barbs");
            intent.putExtra("typeInfo", "Sample Description...");
            startActivity(intent);

But use the correct typeName and typeInfo

Thank you in advance


回答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();
   }
  });



回答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) {

        }
    });
}



回答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);
    }

}


来源:https://stackoverflow.com/questions/48833010/firebase-for-android-studio-how-do-you-loop-through-each-item-in-a-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!