android - Firebase - 如何创建新表/结构?

标签 android firebase firebase-realtime-database

所以我目前正在开发一个 API,它从 Firebase 获取数据,并根据该数据按钮改变其颜色。但是,当我尝试为我的按钮制作一个新结构时。它不会让我做一个。

我在这里尝试了答案:How to insert data of two tables in Firebase?在这里:How can I create an empty table from android app in Firebase?但对我都不起作用。

这是我的示例代码:

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:我在另一个类上创建了用户结构(用于我的登录)。

model

最佳答案

我认为您应该在 firebase 上创建一个新结构,您可以通过单击数据库名称旁边的“+”来完成,这是一个示例:

enter image description here

实现您的方法,它应该会起作用。然后再次连接您的 firebase 数据库。

关于android - Firebase - 如何创建新表/结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47333053/

相关文章:

go - 如何使用 firego 从 firebase 获取值?

Android 在 ListView 中从 URL 加载图像

java - 移动代码后 getLastLocation() 返回 null

Android Firebase Phone Auth 第二次没有收到短信

rest - 如何禁用 Firebase 数据库的 REST API 调用?

android - Google的Firebase身份验证

android - TextView 不会像在屏幕上那样出现在设备上

java - ZXing Result.getRawBytes(),到底是什么?

java - 更改 Firebase 数据时 ListView 不会更新

json - 如何在 React Native 中将 this.props.key 设置到 Firebase 数据库引用中