0

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


I want to create a button in this format:

Android Studio


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

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Benjamin Sommer
  • 1,058
  • 3
  • 15
  • 35

3 Answers3

2

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();
   }
  });
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • How does this add an item to the xml then? – Benjamin Sommer Feb 16 '18 at 18:47
  • the xml is for the layout.. add them in a listview after retrieving them. Im not sure why you are using buttons, but it is better if use a listview in the xml layout. Then in your activity add the items to the listview – Peter Haddad Feb 16 '18 at 18:47
  • I want a Button for each element from firebase, so that it can lead to a new activity with extra information. See the edits now. – Benjamin Sommer Feb 16 '18 at 18:51
  • any suggestions? – Benjamin Sommer Feb 16 '18 at 18:57
  • @marmadukeandbob check the edit, for a button u need to do something like the above, if you want to send data from one activity to another, then use the data that you recieved from the database and add it in `putExtra(..)` to be able to send it to the other activity. You may have to add the `onClickListener` inside the `onDatachange` since it is asynchronous – Peter Haddad Feb 16 '18 at 18:59
  • Does this create a Button? – Benjamin Sommer Feb 16 '18 at 19:02
  • @marmadukeandbob u create the button in the xml layout and in the activity u give it the action that you want.. check this: https://stackoverflow.com/questions/2681640/how-to-add-a-button-in-android – Peter Haddad Feb 16 '18 at 19:03
  • Can I automatically add the Buttons then? – Benjamin Sommer Feb 16 '18 at 19:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165303/discussion-between-peter-haddad-and-marmadukeandbob). – Peter Haddad Feb 16 '18 at 19:03
0

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

        }
    });
}
Vector
  • 3,066
  • 5
  • 27
  • 54
0

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

}
James_Duh
  • 1,321
  • 11
  • 31