我是Firebase和Android Studio的新手,我需要多个节点从我的android应用程序保存到Firebase中的实时数据库,其中包括用户,预算和支出,因为这是费用跟踪应用程序。我已经将用户信息保存在其自己的节点下,但是我不知道如何为预算和支出创建节点。以下代码是我遇到的困难:
@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);
}
答案 0 :(得分:0)
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);
}
获得所有用户输入并完成所有验证后,就需要调用函数sendUserBudgets()
将其保存到数据库中。