我有一个应用,我想一次将多个文本视图发送到Firebase。但是,它一次只能发送一个textview到Firebase。例如,我有以下文本视图:
但是当我将它们提交给Firebase时,我只会得到以下回报:
因此,仅发送了饼干。其他4个文本视图未提交
我尝试使用类构造函数,但无法弄清楚
这是我的数据类
package com.example.aa1172.the_improved_almighty_project;
public class Data {
String foodId;
String foodOrder;
public Data() {
}
public Data(String foodId, String foodOrder) {
this.foodId = foodId;
this.foodOrder = foodOrder;
}
public String getFoodId() {
return foodId;
}
public String getFoodOrder() {
return foodOrder;
}
}
这是我用于数据发送功能的代码,该代码在我的主要活动中
public void send_data() {
String id = dbref.push().getKey();
String chicken = varChickenBurgerTotal.getText().toString();
String kebab = varkebabTotal.getText().toString();
String lamb = varLambDonerTotal.getText().toString();
String biscuits = varBiscuitsTotal.getText().toString();
String icecream = varIceCreamTotal.getText().toString();
if (!TextUtils.isEmpty(chicken) || !TextUtils.isEmpty(kebab) || !TextUtils.isEmpty(icecream) || !TextUtils.isEmpty(biscuits) || !TextUtils.isEmpty(lamb)) {
Toast.makeText(this, "Food order sent", Toast.LENGTH_LONG).show();
// switch (!TextUtils.isEmpty(chicken) || !TextUtils.isEmpty(kebab) || !TextUtils.isEmpty(icecream) || !TextUtils.isEmpty(biscuits) || !TextUtils.isEmpty(lamb))
if (!TextUtils.isEmpty(chicken)) {
Data mydata = new Data(id, chicken);
dbref.child(id).setValue(mydata);
}
if (!TextUtils.isEmpty(kebab)) {
Data mydata = new Data(id, kebab);
dbref.child(id).setValue(mydata);
}
if (!TextUtils.isEmpty(lamb)) {
Data mydata = new Data(id, lamb);
dbref.child(id).setValue(mydata);
}
if (!TextUtils.isEmpty(icecream)) {
Data mydata = new Data(id, icecream);
dbref.child(id).setValue(mydata);
}
if (!TextUtils.isEmpty(biscuits)) {
Data mydata = new Data(id, biscuits);
dbref.child(id).setValue(mydata);
}
} else {
Toast.makeText(this, "No order is being sent", Toast.LENGTH_LONG).show();
}
}
我需要的是当我单击“提交”按钮时将所有文本视图同时发送到Firebase数据库。谢谢
答案 0 :(得分:2)
要仅对数据库进行一次调用,可以执行以下操作:
if (!TextUtils.isEmpty(chicken)) {
mydata = new Data(id, chicken);
}
if (!TextUtils.isEmpty(kebab)) {
mydata = new Data(id, kebab);
}
if (!TextUtils.isEmpty(lamb)) {
mydata = new Data(id, lamb);
}
if (!TextUtils.isEmpty(icecream)) {
mydata = new Data(id, icecream);
}
if (!TextUtils.isEmpty(biscuits)) {
mydata = new Data(id, biscuits);
}
if (mydata != null) {
dbref.child(id).setValue(mydata);
Toast.makeText(this, "Food order sent", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "No order is being sent", Toast.LENGTH_LONG).show();
}
但是,由于您的Data
类只能容纳一种类型,因此只能设置一种类型。
如果要存储多个项目类型,则必须更改Data
类,或存储多个Data
对象。后者是最灵活的,因此您可以使用:
List<Data> orders = new List<Data>();
if (!TextUtils.isEmpty(chicken)) {
orders.add(new Data(id, chicken));
}
if (!TextUtils.isEmpty(kebab)) {
orders.add(new Data(id, kebab));
}
if (!TextUtils.isEmpty(lamb)) {
orders.add(new Data(id, lamb));
}
if (!TextUtils.isEmpty(icecream)) {
orders.add(new Data(id, icecream));
}
if (!TextUtils.isEmpty(biscuits)) {
orders.add(new Data(id, biscuits));
}
System.out.println("Got "+orders.size()+" orders");
if (orders.size() > 0) {
dbref.child(id).setValue(orders);
Toast.makeText(this, "Food order sent", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "No order is being sent", Toast.LENGTH_LONG).show();
}
答案 1 :(得分:0)
创建这样的数据类。
public class Data {
String chicken;
String kebab;
String lamb;
String biscuits;
String icecream;
public Data() {
}
public Data(String chicken, String kebab, String lamb, String biscuits, String icecream) {
this.chicken = chicken;
this.kebab = kebab;
this.lamb= lamb;
this.biscuits= biscuits;
this.icecream = icecream;
}
//set here getter and setter method
}
然后
public void send_data() {
//You can validate your data here as well. On succesfully validation
//create new object of Data and push it frebase
Data mydata = new Data("chicken 5 kg", "kebab 5 kg", "lamb 5 kg", "biscuit 2 pkt", "icecream 300gm")
String id = dbref.push().getKey();
dbref.child(id).setValue(mydata);
}
希望这会有所帮助。
答案 2 :(得分:0)
在Data.class中只有两个构造函数。尝试按照Prafulla Nayak的回答进行即可。同样在您的Data.class中,您拥有一个字符串的FoodOrder。如果要放入数组列表,请尝试使用list <>。