如何在 Java 中将 Arraylist 数据写入 Firebase 数据库?
How can I write Arraylist data to Firebase Database in Java?
我有一个以 firebase 作为后端的 android 应用程序。我有一个 activity,其中使用填充他们想要保存到数据库的回收器视图的列表。
下面是我的数据库的 Firebase Realtime Database 结构的截图:
Values
节点中子项的值是我用来为添加的新数据创建 ID 的值。
我有一个由用户填充的销售对象数组列表,将保存在新的 Sales
节点中,我想使用 Values/Sales
中的值来填充每个项目的 ID在数组列表中。
下面是我保存该数据的代码。但是在数据库中,只保存了数组列表中的最后一项。
ArrayList<Inventory> salesArrayList = new ArrayList<>();
------------------- Code for populating data into the arraylist ----------------------
for (int i = 0; i < salesArrayList.size(); i++){
int finalI = i;
databaseReference.child("Values").child("Sales").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String newCount = String.valueOf(Integer.parseInt(String.valueOf(snapshot.getValue()))+1); // Gets the original value saved and adds 1 to it to be used as the id in the 'Sales' node
DatabaseReference countReference = databaseReference.child("Sales").child(newCount);
countReference.child("date").setValue(currentDate);
countReference.child("name").setValue(salesArrayList.get(finalI).getName());
countReference.child("quantity").setValue(salesArrayList.get(finalI).getQuantity());
countReference.child("unit_price").setValue(salesArrayList.get(finalI).getValue()/salesArrayList.get(finalI).getQuantity());
countReference.child("value").setValue(salesArrayList.get(finalI).getValue());
databaseReference.child("Values").child("Sales").setValue(Integer.parseInt(newCount)); // Updates the number of sales
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
如果数组列表中有 7 个项目,我想要一种方法将它们全部保存在类似于 Purchases
节点的 Sales
中,而不仅仅是数组列表中的最后一个项目.
我不能评论,但我可以回答,但我不知道我的回答是否正确。
如果您只更新数组中的最后一项,在我看来,您在每次迭代时都在覆盖同一个对象,最后只将该对象保存到数据库中。
有附加方法吗?还是文档对此有任何说明?
这对我来说似乎是最可能的原因。
也许您每次都需要 'databaseReference'-对象的新实例。
这行不通(我很惊讶它甚至可以编译):
databaseReference.child("Values").child("Sales")(Integer.parseInt(newCount)); // Updates the number of sales
写回增加的销售额:
snapshot.getReference().setValue(snapshot.getValue(Long.class)+1);
请注意,通常在 Firebase 中使用此类连续数字键是一种反模式。我强烈建议使用 Firebase 的原生 push()
键,它们也总是递增的,但除此之外提供了更强大的保证。要了解更多相关信息,请查看:Best Practices: Arrays in Firebase.
我有一个以 firebase 作为后端的 android 应用程序。我有一个 activity,其中使用填充他们想要保存到数据库的回收器视图的列表。
下面是我的数据库的 Firebase Realtime Database 结构的截图:
Values
节点中子项的值是我用来为添加的新数据创建 ID 的值。
我有一个由用户填充的销售对象数组列表,将保存在新的 Sales
节点中,我想使用 Values/Sales
中的值来填充每个项目的 ID在数组列表中。
下面是我保存该数据的代码。但是在数据库中,只保存了数组列表中的最后一项。
ArrayList<Inventory> salesArrayList = new ArrayList<>();
------------------- Code for populating data into the arraylist ----------------------
for (int i = 0; i < salesArrayList.size(); i++){
int finalI = i;
databaseReference.child("Values").child("Sales").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String newCount = String.valueOf(Integer.parseInt(String.valueOf(snapshot.getValue()))+1); // Gets the original value saved and adds 1 to it to be used as the id in the 'Sales' node
DatabaseReference countReference = databaseReference.child("Sales").child(newCount);
countReference.child("date").setValue(currentDate);
countReference.child("name").setValue(salesArrayList.get(finalI).getName());
countReference.child("quantity").setValue(salesArrayList.get(finalI).getQuantity());
countReference.child("unit_price").setValue(salesArrayList.get(finalI).getValue()/salesArrayList.get(finalI).getQuantity());
countReference.child("value").setValue(salesArrayList.get(finalI).getValue());
databaseReference.child("Values").child("Sales").setValue(Integer.parseInt(newCount)); // Updates the number of sales
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
如果数组列表中有 7 个项目,我想要一种方法将它们全部保存在类似于 Purchases
节点的 Sales
中,而不仅仅是数组列表中的最后一个项目.
我不能评论,但我可以回答,但我不知道我的回答是否正确。
如果您只更新数组中的最后一项,在我看来,您在每次迭代时都在覆盖同一个对象,最后只将该对象保存到数据库中。 有附加方法吗?还是文档对此有任何说明? 这对我来说似乎是最可能的原因。
也许您每次都需要 'databaseReference'-对象的新实例。
这行不通(我很惊讶它甚至可以编译):
databaseReference.child("Values").child("Sales")(Integer.parseInt(newCount)); // Updates the number of sales
写回增加的销售额:
snapshot.getReference().setValue(snapshot.getValue(Long.class)+1);
请注意,通常在 Firebase 中使用此类连续数字键是一种反模式。我强烈建议使用 Firebase 的原生 push()
键,它们也总是递增的,但除此之外提供了更强大的保证。要了解更多相关信息,请查看:Best Practices: Arrays in Firebase.