Firebase:数据库未更新

时间:2017-11-12 12:47:37

标签: android firebase firebase-realtime-database

enter image description here我在Android工作室中第一次使用Firebase。我使用Android助手连接,我想在日期之前添加食物名称,类别和最佳。当我点击添加食物按钮时,没有更新。我已将读写规则更新为true,助手说数据库已连接。 我在某个地方错过了连接吗? Database text

这是我的代码:

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;
}

}

2 个答案:

答案 0 :(得分:0)

你需要做这样的事情:

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

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

database sample

答案 1 :(得分:0)

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

  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. 只需复制并粘贴相应的代码即可。