【发布时间】:2020-12-18 00:58:34
【问题描述】:
我是第一次在 Android Studio 中使用 Firebase。我使用 android 助手连接,我想添加食物名称、类别和保质期。当我单击添加食物按钮时,没有任何更新。我已将读写规则更新为 true,助手说数据库已连接。 我在某处缺少连接吗?
这是我的代码:
public class MainActivity extends AppCompatActivity {
EditText editTextName;
Button buttonAdd;
Spinner spinnerCategory;
EditText editTextDate;
DatabaseReference databaseFood;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseFood = FirebaseDatabase.getInstance().getReference("Food");
editTextName = (EditText) findViewById(R.id.editTextName);
buttonAdd = (Button) findViewById(R.id.buttonAddFood);
spinnerCategory = (Spinner) findViewById(R.id.spinnerCategory);
editTextDate = (EditText) findViewById(R.id.editTextDate);
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
private void addArtist(){
String name = editTextName.getText().toString().trim();
String genre = spinnerCategory.getSelectedItem().toString();
if(!TextUtils.isEmpty(name)){
String id = databaseFood.push().getKey();
Food food = new Food(id, name, genre);
databaseFood.child(id).setValue(food);
Toast.makeText(this, "Food added to Fridge.", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this, "You should enter a name", Toast.LENGTH_LONG).show();
}
}
}
我还添加了一个名为 Food 的 java 类
public class Food {
String foodId;
String foodName;
String foodCategory;
public Food(){
}
public Food(String foodId, String foodName, String foodCategory) {
this.foodId = foodId;
this.foodName = foodName;
this.foodCategory = foodCategory;
}
public String getFoodId() {
return foodId;
}
public String getFoodName() {
return foodName;
}
public String getFoodCategory() {
return foodCategory;
}
}
【问题讨论】:
-
看来您不会调用从
onClick方法更新数据库的方法。 -
我添加了这个,但仍然没有更新
标签: android firebase firebase-realtime-database