简体   繁体   English

android 工作室中的 Firebase 数据检索问题

[英]Firebase data retrieving issue in android studio

I want to retrieve data from firebase and display it on recycle view.我想从 firebase 中检索数据并将其显示在回收视图中。 I provided the correct path for data retrieving.我提供了正确的数据检索路径。 But there is some problem i am unable to find it.但是有一些问题我无法找到它。

This code where i provided the child address.这个代码是我提供孩子地址的地方。

final DatabaseReference nm= FirebaseDatabase.getInstance().getReference("Cart")
            .child("Admin view")
            .child(phoneNo)
            .child("Products");

    nm.addValueEventListener(new ValueEventListener()
    {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists())
            {
                for (DataSnapshot npsnapshot : dataSnapshot.getChildren())
                {
                    cart l=npsnapshot.getValue(cart.class);
                    listData.add(l);

                }

                adapter = new cartAdapterr(listData, AdminShowOrderProductsActivity.this);
                rv.setAdapter(adapter);

            }
            else
            {

              Toast.makeText(AdminShowOrderProductsActivity.this, "No Data for: " + phoneNo, Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });  

截屏

That is the screenshot of my firebase database and emulator.那是我的 firebase 数据库和模拟器的截图。 the phone number in toast message which is also present in firebase database. Toast 消息中的电话号码,它也存在于 firebase 数据库中。 on node Phone number is correct but it shows error.节点上的电话号码正确,但显示错误。

The way you work is correct, but you have a mistake, which is when the data is modified in Firebase, a new cartAdapterr is created and this operation is wrong.你的工作方式是正确的,但是你有一个错误,就是在Firebase中修改数据时,创建了一个new cartAdapterr ,这个操作是错误的。

You must first create an Adapter and then send the data.您必须首先创建一个适配器,然后发送数据。

for example you can create it onCreate and create a method inside the Adapter that receives List <Cart> as Shown below:例如,您可以创建它onCreate并在 Adapter 内部创建一个接收List <Cart>的方法,如下所示:

       @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
    
            //..

            adapter = new cartAdapterr(this);
            loadDataFirebase():
    
    }


        void loadDataFirebase(){


         final DatabaseReference nm= FirebaseDatabase.getInstance().getReference("Cart")
            .child("Admin view")
            .child(phoneNo)
            .child("Products");

    nm.addValueEventListener(new ValueEventListener()
    {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists())
            {
                for (DataSnapshot npsnapshot : dataSnapshot.getChildren())
                {
                    cart l=npsnapshot.getValue(cart.class);
                    listData.add(l);

                }

                adapter.setDataList(listData);

            }
            else
            {

              Toast.makeText(AdminShowOrderProductsActivity.this, "No Data for: " + phoneNo, Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    }); 
}

In Adapter you have to create this setDataList (List<Cart> cartItems) :在适配器中,您必须创建此setDataList (List<Cart> cartItems)

 public void setDataList (List<Cart> cartItems ) {

        this.cartItems = cartItems;
        
        notifyDataSetChanged();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM