gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:29:36 25 4
gpt4 key购买 nike

我刚开始使用 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);
}

最佳答案

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() 将其保存到数据库。

关于java - 将多个节点存储到 Firebase 实时数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61147949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com