java - 在 Firebase 中使用矩阵

标签 java android firebase

设置一个 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++;
        }

    }

最佳答案

来自 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>>();

关于java - 在 Firebase 中使用矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38062324/

相关文章:

android - Firebase 身份验证 - 发生网络错误(例如超时、连接中断或主机无法访问)

java - Spring boot Maven MultiModule Rest api 调用

android - 在 Android Canvas 上绘制位图的一部分

java - 如何正确关闭 Tomcat 上的 JAX-WS Spring 应用程序?

java - Android 最佳实践 - View /Activity

Android 应用程序通过套接字与 Android native 二进制文件通信

Android FirebaseRecyclerAdapter populateViewHolder() 永远不会被调用

javascript - Firestore : Multiple conditional where clauses

java - 普通 Java 持久性和 JavaFX 表示层 : approach for class design?

java - 如何从Java中的main调用非静态方法?