Ann*_*oon 2 android firebase firebase-realtime-database
所以我目前正在开发一个 API,它从 Firebase 获取数据,并根据该数据按钮改变其颜色。然而,当我试图为我的按钮制作一个新的结构时。它不会让我做一个。
我在这里尝试了答案:如何在 Firebase 中插入两个表的数据?在这里:如何从 Firebase 中的 android 应用程序创建一个空表?但都不适合我。
这是我的示例代码:
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.
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
PS:我在另一个类上创建了用户结构(用于我的登录)。
我认为您应该在 firebase 上创建一个新结构,您可以通过单击数据库名称旁边的“+”来实现,这是一个示例:
实施您的方法,它应该可以工作。然后再次连接您的 firebase 数据库。
| 归档时间: |
|
| 查看次数: |
16984 次 |
| 最近记录: |