java – 在firebase中使用矩阵

一段时间后设置一个int矩阵int [] [] Matrix = new int [4] [4]我无法将它上传到firebase m
Database.child(“room_1”).setValue(Matrix);因为不支持数组.

我使用int [] []的原因是能够使用Matrix [2] [3] = 1添加整数

哪些数据类型与firebase兼容并且可以处理这些类型的添加?

谢谢.

我无法按照我想要的方式使用list和Long,所以我使用int matrix [] []并使用setValue(matrix [index] [index2])更新数据库.

为了将数据库放入矩阵,我使用了两个while循环嵌套,它检查每个值并将其放入矩阵中
以下函数获取Object

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++;
        }

    }

最佳答案 从
firebase documentation起.

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:

  1. Pass types that correspond to the available JSON types as follows:

    • String
    • Long
    • Double
    • Boolean
    • Map
    • List
  2. 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>>();
点赞