设置一个 int 矩阵一段时间后 int[][] Matrix = new int[4][4]
我无法将它上传到 firebase mDatabase.child("room_1")。 setValue(Matrix);
因为不支持数组。
我使用 int[][]
的原因是能够使用 Matrix[2][3] += 1
添加整数
哪些数据类型与 firebase 兼容并且可以处理这些类型的添加?
谢谢。
我无法让它按照我想要的方式使用列表或 Long,所以我使用了 int matrix[][]
并使用 setValue(matrix[index][ index2])
在一个一个的基础上。
为了将数据库放入矩阵中,我使用了两个嵌套的 while 循环来检查每个值并将其放入矩阵中 下面的函数获取对象
public void SyncWDB(Object dbValue) {
ArrayList aList = (ArrayList) dbValue;
//I hardcoded the matrix dimensions just for testing
int e = 0;
int i = 0;
while (e < 5) {
ArrayList temp = (ArrayList) aList.get(e);
while (i < 4) {
Long temp2 = Long.parseLong(String.valueOf(temp.get(i)));
int temp3 = temp2.intValue();
// Matrix is a class variable
Matrix[e][i] = temp3;
i++;
}
i = 0;
e++;
}
}
最佳答案
Basic write operations
For basic write operations, you can use
setValue()
to save data to a specified reference, replacing any existing data at that path. You can use this method to:
Pass types that correspond to the available JSON types as follows:
- String
- Long
- Double
- Boolean
- Map
- List
Pass a custom Java object, if the class that defines it has a default constructor that takes no arguments and has public getters for the properties to be assigned.
我强烈建议您选择第二个选项,并且再次 firebase documentation将帮助您了解如何去做。
如果您仍想使用“直接设置”,则可以使用二维列表。
List<List<Integer>> listOfLists = new ArrayList<List<Integer>>();
关于java - 在 Firebase 中使用矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38062324/