我的应用程序已连接但未更新,如图所示,您可以看到我是 Android 应用程序开发的新手。我想在 Firebase 中保存和检索我的按钮单击事件。我的按钮点击Android代码是:
View.OnClickListener btnOnOffClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
String onoff="";
switch(v.getId()){
case R.id.living_btn:
if (click ==0){
click=1;
onoff = "/SWITCH-01=ON";
living_btn.setBackgroundResource(R.drawable.livingroom);
}else{
click=0;
onoff = "/SWITCH-01=OFF";
living_btn.setBackgroundResource(R.drawable.livingroom_off);
}
break;
case R.id.kitchen_btn:
if (click ==0){
click=1;
onoff = "/SWITCH-02=ON";
kitchen_btn.setBackgroundResource(R.drawable.kitchen);
}else{
click=0;
onoff = "/SWITCH-02=OFF";
kitchen_btn.setBackgroundResource(R.drawable.kitchen_off);
}
break;
case R.id.bedroom1_btn:
if (click ==0){
click=1;
onoff = "/SWITCH-03=ON";
bedroom1_btn.setBackgroundResource(R.drawable.bed_room);
}else{
click=0;
onoff = "/SWITCH-03=OFF";
bedroom1_btn.setBackgroundResource(R.drawable.bed_room_off);
}
break;
case R.id.bedroom2_btn:
if (click ==0){
click=1;
onoff = "/SWITCH-04=ON";
bedroom2_btn.setBackgroundResource(R.drawable.bedroom2);
}else{
click=0;
onoff = "/SWITCH-04=OFF";
bedroom2_btn.setBackgroundResource(R.drawable.bedroom2_off);
}
break;
case R.id.bathroom_btn:
if (click ==0){
click=1;
onoff = "/SWITCH-05=ON";
bathroom_btn.setBackgroundResource(R.drawable.bathroom);
}else{
click=0;
onoff = "/SWITCH-05=OFF";
bathroom_btn.setBackgroundResource(R.drawable.bathroom_off);
}
break;
case R.id.toilet_btn:
if (click ==0){
click=1;
onoff = "/SWITCH-06=ON";
toilet_btn.setBackgroundResource(R.drawable.toi_on);
}else{
click=0;
onoff = "/SWITCH-06=OFF";
toilet_btn.setBackgroundResource(R.drawable.toi_off);
}
break;
}
我可以将v.getID
“数据库引用”和“设置值”添加到onoff
(变量)吗?如果没有,那么如何添加按钮点击数据?此外,如何通过“数据更改”事件检索数据。
请帮忙。