【问题标题】:Firebase: Database not updatingFirebase:数据库未更新
【发布时间】: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


【解决方案1】:

你需要这样做:

databaseFood = FirebaseDatabase.getInstance().getReference("Food").push();

private void addArtist(){
String name = editTextName.getText().toString().trim();
String genre = spinnerCategory.getSelectedItem().toString();
if(!TextUtils.isEmpty(name)){

databaseFood.child("name").setValue(name);
databaseFood.child("genre").setValue(genre);
 }}

你在数据库中的那种方式:

  Food:{
   pushrandomid:{
       name: nuggets
       genre: chicken
         }
      }

类中同样添加setter,点击alt+ins,添加setter

你的数据库应该是这样的:

【讨论】:

  • 这会放在 OnCreate 方法中吗?
  • setValues 可以进入private void addArtist(){databaseFood = FirebaseDatabase.getInstance().getReference("Food").push(); 可以进入 onCreate。与您所做的相同,只需相应更改即可。但是您可能必须这样做setValue(food.getFoodName())
  • 数据库仍然没有任何反应
  • 我写了“databaseFood.child("name").setValue(name); databaseFood.child("genre").setValue(genre);"下私虚加食。在我的数据库中什么都没有,它没有显示任何文字
  • 数据库中有根节点吗?您的意思是在 addArtist 下,上面的代码中没有 private void add food
【解决方案2】:

我认为以下答案将有助于解决所有问题,并提供一些有效的方法来执行此操作。 ;-)

  1. 按如下方式重新创建您的 Food 类:-

.

public class Foods {                                                             
String foodName;
String foodCategory;

public  Food(){

}

public Food(String foodName, String foodCategory) {
    this.foodName = foodName;
    this.foodCategory = foodCategory;
}

public String getFoodName() {
    return foodName;
}

public String getFoodCategory() {
    return foodCategory;
}
  1. 主代码格式如下。

.

public class MainAcivity extends AppCompactActivity {
EditText editTextName;
Button buttonAdd;
Spinner spinnerCategory;
EditText editTextDate;                                                
DatabaseReference databaseFood;

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


    String name = editTextName.getText().toString().trim();
    String category = spinnerCategory.getSelectedItem().toString();
    buttonAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            uploadFoodData();

        }
    });

}


Private void uploadFoodData(){
    databaseFood = FirebaseDataBase.getInstance.getReference();
    String id = databaseFood.push().getKey();
    Foods food = new Foods(name, category);
    databaseFood.child(id).setValue(food);
}
  1. 只需复制并粘贴相应的代码即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多