简体   繁体   English

将多个节点存储到 Firebase 实时数据库

[英]Store multiple nodes to Firebase Realtime Database

I'm new to using Firebase and Android Studio and I need multiple nodes to save to a realtime database in Firebase from my android app, these include users, budgets and spending as it's an expense tracking app. I'm new to using Firebase and Android Studio and I need multiple nodes to save to a realtime database in Firebase from my android app, these include users, budgets and spending as it's an expense tracking app. I've got the user information to save under its own node but I can't figure out how to create nodes for budget and spending.我已经将用户信息保存在自己的节点下,但我不知道如何为预算和支出创建节点。 The code below is what I'm having difficulty with:下面的代码是我遇到的困难:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_categories);
    setupUIViews();
    firebaseAuth = getInstance();
    mDatabase = FirebaseDatabase.getInstance().getReference();

    btn_subCat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(validate()){
                sendUserBudgets();
                Toast.makeText(Categories.this, "Completed!", Toast.LENGTH_LONG).show();
                finish();
                startActivity(new Intent(Categories.this, Menu.class ));
            }else{
                Toast.makeText(Categories.this, "Submission failed", Toast.LENGTH_LONG).show();
            }
        }
    });
}
private void setupUIViews() {
    travel_input = (EditText)findViewById(R.id.travel_input);
    entertainment_input = (EditText)findViewById(R.id.entertainment_input);
    fitness_input = (EditText)findViewById(R.id.fitness_input);
    beauty_input = (EditText)findViewById(R.id.beauty_input);
    clothes_input = (EditText)findViewById(R.id.clothes_input);
    holiday_input = (EditText)findViewById(R.id.holiday_input);
    food_input = (EditText)findViewById(R.id.food_input);
    mobile_input = (EditText)findViewById(R.id.mobile_input);
    btn_subCat = (Button)findViewById(R.id.btn_subCat);

}

private Boolean validate() {
    boolean result = false;
    travel_budget = Double.parseDouble(travel_input.getText().toString().trim());
    entertainment_budget = Double.parseDouble(entertainment_input.getText().toString().trim());
    fitness_budget = Double.parseDouble(fitness_input.getText().toString().trim());
    beauty_budget = Double.parseDouble(beauty_input.getText().toString().trim());
    clothes_budget = Double.parseDouble(clothes_input.getText().toString().trim());
    holiday_budget = Double.parseDouble(holiday_input.getText().toString().trim());
    food_budget = Double.parseDouble(food_input.getText().toString().trim());
    mobile_budget = Double.parseDouble(mobile_input.getText().toString().trim());

    if(travel_budget.equals(null) || entertainment_budget.equals(null) || fitness_budget.equals(null) || beauty_budget.equals(null) || clothes_budget.equals(null) || holiday_budget.equals(null) || food_budget.equals(null) ||
            mobile_budget.equals(null)){
        Toast.makeText(Categories.this, "Please enter all fields", Toast.LENGTH_LONG).show();
    } else {
        result = true;
    }
    return result;
}

private void sendUserBudgets() {
    String currentUserID = firebaseAuth.getUid();
    CategoriesDB catDb = new CategoriesDB(travel_budget, entertainment_budget, fitness_budget, beauty_budget, clothes_budget, holiday_budget, food_budget, mobile_budget);
    mDatabase.child("User").child(currentUserID).child("Budgets").setValue(catDb);
}
private void validate() {
    boolean result = false;
    travel_budget = Double.parseDouble(travel_input.getText().toString().trim());
    entertainment_budget = Double.parseDouble(entertainment_input.getText().toString().trim());
    fitness_budget = Double.parseDouble(fitness_input.getText().toString().trim());
    beauty_budget = Double.parseDouble(beauty_input.getText().toString().trim());
    clothes_budget = Double.parseDouble(clothes_input.getText().toString().trim());
    holiday_budget = Double.parseDouble(holiday_input.getText().toString().trim());
    food_budget = Double.parseDouble(food_input.getText().toString().trim());
    mobile_budget = Double.parseDouble(mobile_input.getText().toString().trim());

    if(travel_budget.equals(null) || entertainment_budget.equals(null) || fitness_budget.equals(null) || beauty_budget.equals(null) || clothes_budget.equals(null) || holiday_budget.equals(null) || food_budget.equals(null) ||
            mobile_budget.equals(null)){
        Toast.makeText(Categories.this, "Please enter all fields", Toast.LENGTH_LONG).show();
    } else {
        sendUserBudgets(travel_budget, fitness_budget, beauty_budget, clothes_budget, holiday_budget, food_budget, mobile_budget);
    }
}

private void sendUserBudgets(double travel_budget, double fitness_budget, double beauty_budget, double clothes_budget, double holiday_budget, doublefood_budget, double mobile_budget) {
    String currentUserID = firebaseAuth.getUid();
    CategoriesDB catDb = new CategoriesDB(travel_budget, entertainment_budget, fitness_budget, beauty_budget, clothes_budget, holiday_budget, food_budget, mobile_budget);
    mDatabase.child("User").child(currentUserID).child("Budgets").setValue(catDb);
}

Here once you have obtained all the user inputs and have done all the validations, you need to call the function sendUserBudgets() to save it to the database.在这里,一旦您获得了所有用户输入并完成了所有验证,您需要调用 function sendUserBudgets()将其保存到数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM