我是Android Studio的新手,我需要有多个节点才能在Firebase中存储数据,我目前有一个用于存储登录信息的用户节点,但是我需要一个节点来设置预算。这是我目前为.java以及getter和setter编写的代码。我希望用户为每个类别输入一个金额,然后将其保存到他们的个人资料中,以便以后可以将他们的实际支出与设定的预算进行比较。
package com.example.traktrail;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import static com.google.firebase.auth.FirebaseAuth.getInstance;
public class Categories extends AppCompatActivity {
private FirebaseAuth firebaseAuth;
DatabaseReference mDatabase;
EditText travel_input, entertainment_input, fitness_input, beauty_input, clothes_input,
holiday_input, food_input, mobile_input;
Double travel_budget, entertainment_budget, fitness_budget, beauty_budget, clothes_budget,
holiday_budget, food_budget, mobile_budget;
Button btn_subCat;
FirebaseDatabase database;
@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);
}
package com.example.traktrail;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import static com.google.firebase.auth.FirebaseAuth.getInstance;
public class Categories extends AppCompatActivity {
private FirebaseAuth firebaseAuth;
DatabaseReference mDatabase;
EditText travel_input, entertainment_input, fitness_input, beauty_input, clothes_input,
holiday_input, food_input, mobile_input;
Double travel_budget, entertainment_budget, fitness_budget, beauty_budget, clothes_budget,
holiday_budget, food_budget, mobile_budget;
Button btn_subCat;
FirebaseDatabase database;
@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);
}
public void goBack(View view){startActivity(new Intent(Categories.this, Menu.class));}
}
getters and setters :
package com.example.traktrail;
public class CategoriesDB {
private Double travel_budget;
private Double entertainment_budget;
private Double fitness_budget;
private Double beauty_budget;
private Double clothes_budget;
private Double holiday_budget;
private Double food_budget;
private Double mobile_budget;
private String email_User;
public CategoriesDB(Double travel_input, Double entertainment_input, Double fitness_input, Double
beauty_input, Double clothes_input, Double holiday_input, Double food_input, Double mobile_input,
String email){
this.travel_budget = travel_input;
this.entertainment_budget = entertainment_input;
this.fitness_budget = fitness_input;
this.beauty_budget = beauty_input;
this.clothes_budget = clothes_input;
this.holiday_budget = holiday_input;
this.food_budget = food_input;
this.mobile_budget = mobile_input;
this.email_User = email;
}
public CategoriesDB(Double travel_budget, Double entertainment_budget, Double fitness_budget, Double
beauty_budget, Double clothes_budget, Double holiday_budget, Double food_budget, Double
mobile_budget) {
}
public Double getTravel_budget() { return travel_budget; }
public void setTravel_budget(Double travel_budget) { this.travel_budget = travel_budget; }
public Double getEntertainment_budget() { return entertainment_budget; }
public void setEntertainment_budget(Double entertainment_budget) { this.entertainment_budget =
entertainment_budget; }
public Double getFitness_budget() { return fitness_budget; }
public void setFitness_budget(Double fitness_budget) { this.fitness_budget = fitness_budget; }
public Double getBeauty_budget() { return beauty_budget; }
public void setBeauty_budget(Double beauty_budget) { this.beauty_budget = beauty_budget; }
public Double getClothes_budget() { return clothes_budget; }
public void setClothes_budget(Double clothes_budget) { this.clothes_budget = clothes_budget; }
public Double getHoliday_budget() { return holiday_budget; }
public void setHoliday_budget(Double holiday_budget) { this.holiday_budget = holiday_budget; }
public Double getFood_budget() { return food_budget; }
public void setFood_budget(Double food_budget) { this.food_budget = food_budget; }
public Double getMobile_budget() { return mobile_budget; }
public void setMobile_budget(Double mobile_budget) { this.mobile_budget = mobile_budget; }
public String getEmail_User() { return email_User; }
public void setEmail_User(String email_User) { this.email_User = email_User; }
}