Ann Yoon
Ann Yoon

Reputation: 157

Firebase - How do I make a new table/structure?

So I am currently developing an API which gets data from Firebase and depending on that data the button changes its color. However, when I tried to make a new structure for my buttons. It won't let me make one.

I tried the answers here: How to insert data of two tables in Firebase? and here: How can I create an empty table from android app in Firebase? but neither did work for me.

Here's my sample code:

public class Normal extends AppCompatActivity {

    Button suite, normal, room1;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;
    private DatabaseReference mFirebaseDatabase1;
    private FirebaseDatabase mFirebaseInstance;

    //FIREBASE AUTH FIELDS

    DatabaseReference mSearchedLocationReference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_normal);

        mFirebaseInstance = FirebaseDatabase.getInstance();

        //FIREBASE
        mFirebaseDatabase1 = mFirebaseInstance.getReference("Rooms");
        mSearchedLocationReference = mFirebaseDatabase1.child("Room1").child("RoomStatus");


        //ASSIGN ID's

        room1 = (Button) findViewById(R.id.room2);


        room1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mFirebaseDatabase1.child("Rooms").child("Room1").child("RoomStatus").setValue("Green");
                startActivity(new Intent(Normal.this, room2.class));
            }
        });


        mSearchedLocationReference.addValueEventListener(new ValueEventListener() { //attach listener

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) { //something changed!
                for (DataSnapshot locationSnapshot : dataSnapshot.getChildren()) {
                    String location = locationSnapshot.getValue().toString();
                    Log.d("Locations updated", "location: " + location); //log
                    if ( location.equals("Green")){
                        room1.setBackgroundColor(Color.GREEN);
                    }else if ( location.equals("Red")){
                        room1.setBackgroundColor(Color.RED);

                    }
                    else{
                        room1.setBackgroundColor(Color.YELLOW);

                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) { //update UI here if error occurred.

            }
        });

    }
}

PS: I created the User structure (which is for my login) on another class.

model

Upvotes: 2

Views: 25128

Answers (3)

Rekt
Rekt

Reputation: 399

I think you should make a new structure at firebase you can do it by clicking the "+" beside your DB Name, here's an example:

enter image description here

Implement your methods and it should work. Then Connect again your firebase database.

Upvotes: 5

Hasan Bou Taam
Hasan Bou Taam

Reputation: 4035

I think the problem is when you are changing the value when (room 1) is clicked. You already declared this reference like this:

mFirebaseDatabase1 = mFirebaseInstance.getReference("Rooms");

when you clicked the button (room 1) you triggered this reference to change value :

mFirebaseDatabase1.child("Rooms").child("Room1").child("RoomStatus").setValue("Green");

In other words

you are saying that you want to access

Rooms/Rooms/Room1/RoomStatus/Green

but you should be looking for this

Rooms/Room1/RoomStatus/Green

you have an extra child called Rooms that is repeated and causing the problem

Possible Solution

do this in the click listener of room 1

   room1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mFirebaseDatabase1.child("Room1").child("RoomStatus").setValue("Green");
            startActivity(new Intent(Normal.this, room2.class));
        }
    });

I just removed the extra(.child("Rooms")).

Upvotes: 1

Waqar Khan
Waqar Khan

Reputation: 42

What i think is going wrong is that an array is returning and its last item is green

for (DataSnapshot locationSnapshot : dataSnapshot.getChildren())

that is not letting you change the color of the button .

we will be able to help you if you show the model of your database .

Upvotes: 0

Related Questions