Firebase在Android Studio中的数据检索问题

huangapple go评论81阅读模式
英文:

Firebase data retrieving issue in android studio

问题

我想从Firebase检索数据并在回收视图上显示它。我已提供了正确的数据检索路径。但是存在一些问题,我无法找到它。

这是我提供子地址的代码。

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) {

    }
});

这是我的Firebase数据库和模拟器的屏幕截图。吐司消息中的电话号码也存在于Firebase数据库中。在节点Phone number上是正确的,但它显示错误。

英文:

I want to retrieve data from firebase and display it on recycle view. 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) {

        }
    });  

Firebase在Android Studio中的数据检索问题

That is the screenshot of my firebase database and emulator. the phone number in toast message which is also present in firebase database. on node Phone number is correct but it shows error.

答案1

得分: 1

你的工作方式是正确的,但是你有一个错误,即当数据在Firebase中被修改时,会创建一个新的cartAdapterr,这个操作是错误的。

你必须首先创建一个适配器(Adapter),然后再发送数据。

例如,你可以在onCreate中创建它,并在适配器内创建一个接收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) {

        }
    }); 
}

在适配器中,你需要创建这个setDataList(List<Cart> cartItems)方法:

public void setDataList(List<Cart> cartItems) {
    this.cartItems = cartItems;
    notifyDataSetChanged();
}
英文:

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.

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 &lt;Cart&gt; as Shown below :

       @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(&quot;Cart&quot;)
            .child(&quot;Admin view&quot;)
            .child(phoneNo)
            .child(&quot;Products&quot;);

    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, &quot;No Data for: &quot; + phoneNo, Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    }); 
}

> In Adapter you have to create this setDataList (List&lt;Cart&gt; cartItems) :

 public void setDataList (List&lt;Cart&gt; cartItems ) {

        this.cartItems = cartItems;
        
        notifyDataSetChanged();
    }

huangapple
  • 本文由 发表于 2020年8月7日 17:56:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63299419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定