猿问

如何在数组中安排 firebase 检索?

我正在尝试检索并显示用户过去的付款。为此,我使用了一个 Recycler-view,它从数组中获取信息并显示所有过去的交易。

从上面的数据库(在图像中),我想检索此数据并将其存储在以下格式的数组中:


amount[]={"100","200"};             

rid[]={"123456",123456"};             

date_time[]={"16 Aug 2019 14:10:26","16 Aug 2019 14:12:26"};

以下是我需要帮助的 Android Studio 代码:


      FirebaseDatabase.getInstance().getReference().child("payments").child(firebaseAuth.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {

            @Override

            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                //How to retrieve the data in above array format

            }


            @Override

            public void onCancelled(@NonNull DatabaseError databaseError) {


            }

        });


繁星淼淼
浏览 116回答 2
2回答

潇潇雨雨

您可以使用List:List<String> amountList&nbsp; &nbsp;= new ArrayList<>();List<String> ridList&nbsp; &nbsp; &nbsp; = new ArrayList<>();List<String> dateTimeList = new ArrayList<>();&nbsp; &nbsp; &nbsp;FirebaseDatabase.getInstance().getReference().child("payments").child(firebaseAuth.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {&nbsp; &nbsp;@Override&nbsp; &nbsp;public void onDataChange(@NonNull DataSnapshot dataSnapshot) {&nbsp; &nbsp; for(DataSnapshot ds : dataSnapshot.getChildren()){&nbsp; &nbsp; &nbsp; &nbsp; String amount&nbsp; &nbsp; = ds.child("amount").getValue(String.class);&nbsp; &nbsp; &nbsp; &nbsp; String rid&nbsp; &nbsp; &nbsp; &nbsp;= ds.child("rid").getValue(String.class);&nbsp; &nbsp; &nbsp; &nbsp; String dateTime&nbsp; = ds.child("date_time").getValue(String.class);&nbsp; &nbsp; &nbsp; &nbsp; amountList.add(amount);&nbsp; &nbsp; &nbsp; &nbsp; ridList.add(rid);&nbsp; &nbsp; &nbsp; &nbsp; dateTimeList.add(dateTime);&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;}&nbsp; &nbsp;@Override&nbsp; &nbsp;public void onCancelled(@NonNull DatabaseError databaseError) {&nbsp; &nbsp; }&nbsp; });你databaseReference在 nodeuid然后你在里面循环uid并获取每个属性的值,然后你可以将它们添加到 a 中List。

拉丁的传说

int count = (int) datasnapshot.getChildrenCount();String amount[]=new String[count];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;....// initialize other arraysint i=0; //loop counterfor(Datasnapshot post: datasnapshot.getChildren){&nbsp;...&nbsp;//iterate every value and insert into respective array}除了这个之外,我在单个 Firebase 查询中看不到任何方式,或者您可以改用 List<> 。
随时随地看视频慕课网APP

相关分类

Java
我要回答