在2D ArrayList Android中从实时数据库检索字符串

时间:2020-08-14 07:00:57

标签: java android firebase firebase-realtime-database

我有一个像这样的Firebase实时数据库结构:

Firebase Structure

如何使用Java将其检索到2D ArrayList中。

col3

2 个答案:

答案 0 :(得分:1)

正如@SardorbekKhujaev在回答中提到的那样,数组是固定的数据类型。要解决此问题,我们需要使用ArrayList,因为可以动态增加大小。填充ArrayList之后,我们可以简单地将其转换为2D数组,如以下代码行所示:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference choicesRef = rootRef.child("choices");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<String[]> choices = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            int size = (int) ds.getChildrenCount();
            String[] choice = new String[size];
            int count = 0;
            for(DataSnapshot dSnapshot : ds.getChildren()) {
                choice[count++] = dSnapshot.getValue(String.class);
            }
            choices.add(choice);
        }
        String choicesArray[][] = new String[choices.size()][];
        for (int j = 0; j < choices.size(); j++) {
            choicesArray[j] = choices.get(j);
        }
        //Test the array
        for(String[] array : choicesArray) {
            Log.d(TAG, Arrays.toString(array));
        }
        //Do what you need to do with your 2D array
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); //Don't ignore potential errors!
    }
};
choicesRef.addListenerForSingleValueEvent(valueEventListener);

此代码的结果将是您要查找的确切2D数组。

答案 1 :(得分:0)

您要使用2D数组,因为数组不是动态的,这会使代码更加复杂。考虑到Firebase数据是动态的,并且考虑到Java中数组列表的一般便利性,我建议您使用ArrayList。 此解决方案着重于问题所在-2D阵列

private String[][] mChoices;
private void initializeMChoices(int i, int j) {
    mChoices = new String[i][j];
}

在需要用String值填充数组的任何地方,请粘贴以下代码:

        //Get the reference to the "choices" node:
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference("choices");
        ref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                // We first go through the whole node to count it's child nodes
                int i = 0, j = 0;
                while(snapshot.child(Integer.toString(i)).exists())
                {
                    j = 0;
                    while(snapshot.child(Integer.toString(i)).child(Integer.toString(i)).exists())
                    {
                        j++;
                    }
                    i++;
                }
                // we have the numbers, now we initialize the array
                initializeMChoices(i, j);
            //we go through the whole node to put child-node values in the 2D array
            i = 0;
            j = 0;
            while(snapshot.child(Integer.toString(i)).exists())
            {
                while(snapshot.child(Integer.toString(i)).child(Integer.toString(i)).exists())
                {
                    j++;
                    mChoices[i][j] = snapshot.child(Integer.toString(i)).child(Integer.toString(i)).toString();
                }
                i++;
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });