Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
firebase android中的嵌套列表对象_Android_Firebase_Arraylist_Firebase Realtime Database_Nested - Fatal编程技术网

firebase android中的嵌套列表对象

firebase android中的嵌套列表对象,android,firebase,arraylist,firebase-realtime-database,nested,Android,Firebase,Arraylist,Firebase Realtime Database,Nested,这是我的数据库结构 order key address cart node with 3 childs phone total etc 我尝试了所有方法进入已创建的cart对象,但当它给我一个错误时,当我尝试在recyclerview中检索它时,它只返回第一个“0”子对象,其他什么都没有。我尝试过使用hashmaps和ArrayList,还提供了recyclerview适配器来显示数据的检索 //this is my pojo below. public

这是我的数据库结构

order
  key
    address
    cart node with 3 childs
    phone
    total etc

我尝试了所有方法进入已创建的cart对象,但当它给我一个错误时,当我尝试在recyclerview中检索它时,它只返回第一个“0”子对象,其他什么都没有。我尝试过使用hashmaps和ArrayList,还提供了recyclerview适配器来显示数据的检索

//this is my pojo below.

public class Order {

private String phone;
private String address;
private String time;
private String date;
private String message;
private String total;
private HashMap<String,Cart> cart;
private String tax;
private String status;

public Order() {
}

//getter and setter


//code for looping over the child node
  orderDB.child("orders").child(currentUser).addValueEventListener(new 
  ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                for (DataSnapshot orderSnapshot : dataSnapshot.getChildren()) {
                    Order order = orderSnapshot.getValue(Order.class);
                    onGoingOrderList.add(order);                   
                    rvOnGoingOrders.setAdapter(onGoingOrderadapter);

                }

            }
        }

//code for my adapter
public void onBindViewHolder(OngoingOrderViewholder holder, int position) {
   holder.tvProductnameOngoingOrders.setText(onGoingOrderArrayList.get(position).getCart().get(position).getName());
//下面是我的pojo。
公共阶级秩序{
私人电话;
私有字符串地址;
私有字符串时间;
私有字符串日期;
私有字符串消息;
私有字符串总数;
私有HashMap购物车;
私税;
私有字符串状态;
公共秩序(){
}
//接二连三
//在子节点上循环的代码
orderDB.child(“orders”).child(currentUser).addValueEventListener(新建
ValueEventListener(){
@凌驾
public void onDataChange(@NonNull DataSnapshot DataSnapshot){
if(dataSnapshot.exists()){
对于(DataSnapshot orderSnapshot:DataSnapshot.getChildren()){
Order Order=orderSnapshot.getValue(Order.class);
onGoingOrderList.add(订单);
RVONGOIGORDERS.设置适配器(ONGOIGORDERA适配器);
}
}
}
//我的适配器的代码
BindViewHolder上的公共无效(OngoingOrderViewholder,内部位置){
holder.tvProductnameOngoingOrders.setText(OnGoInGorderaryList.get(position).getCart().get(position).getName());
如果我将数据检索到hashmap中,就会出现这个错误

代码中的问题是在for循环中设置适配器。要解决此问题,请移动以下代码行:

rvOnGoingOrders.setAdapter(onGoingOrderadapter);

超出for循环,否则您将只显示一项。

请将数据结构/代码作为文本而不是图像/链接发布。将其更改为文本。“它给我一个错误,当我尝试检索它时”什么错误?还请注意,您将要调用
onGoingOrderList.notifyDataSetChanged()
在onDataChange的末尾。我认为从firebase获取数据的最佳方法是在for循环中创建一个arraylist,并通过pojo类添加所有数据。然后使用arraylist.get(位置)在recyclerview适配器中检索它.getProperty。我已经填充了许多recyclerviews,但这次它似乎根本不起作用。我已经编辑了代码,并将setAdapter放入for循环中,这是检索数据的标准,并且做了很多次,但是在POJO中使用arraylist作为变量,它似乎不起作用。不幸的是,它不起作用仍然显示在屏幕上仅1项。使用您的逻辑编辑了我的问题。如果您试图使用
Log.d(“TAG”,onGoingOrderList.toString());
在这行代码之后
rvOnGoingOrders.setAdapter(onGoingOrderadapter)
它打印什么?列表中有项还是空的?不,它不是空的,因为我正在从列表中检索一项。请查看设置适配器的内容。我认为您应该查看一下我的数据结构。如果可能,您可以为orders节点建议一种更好的数据结构构建方法。