Solution 1 :
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 <Cart>
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("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)
:
public void setDataList (List<Cart> cartItems ) {
this.cartItems = cartItems;
notifyDataSetChanged();
}
Problem :
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) {
}
});
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.
Comments
Comment posted by Alex Mamo
What’s the error? And please also add the content of your
Comment posted by touqeer javaid
Thank you i solved this issue actually i saved phone number in Firebase and retrieved it this thing work for me.. before i was using edit text number it was same but didn’t work i don’t why