Skip to content Skip to sidebar Skip to footer

Firebase Database Load Data Into Listview

I have this DB structure: { 'customers' : { '-L-OcgJ0kwTNSm6HoSvG' : { 'address' : 'Test Alamat', 'birthday' : '1990-12-03', 'email' : 'Dodi@g

Solution 1:

I recommend to you to create custom Adapter and to use a RecyclerView (it is faster and better than a ListView )

Something like this:

publicclassCustomerAdapterextendsRecyclerView.Adapter<CustomerAdapter.MessageViewHolder> {
private List<Customer> customerList;                                                                        
private Context context;



publicCustomerAdapter(List<Customer> customerList, Context context) {
    this.customerList= customerList;
    this.context = context;
}

@Overridepublic CustomerAdapter.MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
   Viewv= LayoutInflater.from(parent.getContext())
                   .inflate(R.layout.your_layout, parent, false);
         returnnewCustomerAdapter.MessageViewHolder(v);
} 
publicclassCustomerViewHolderextendsRecyclerView.ViewHolder {
     public TextView customername, customeraddress, customerphone;

     publicCustomerViewHolder(View view) {
          super(view);
          customername = view.findViewById(R.id.txtCustomerName);
          customeraddress = view.findViewById(R.id.txtCustomerAddress);
          customerphone = view.findViewById(R.id.txtCustomerPhone);
     }
}

@OverridepublicintgetItemCount() {
    return customerList.size();
}

@OverridepublicvoidonBindViewHolder(final CustomerAdapter.MessageViewHolder holder, finalint position) {
      holder.customername.setText(customerList.get(position).getName;
      holder.customeraddress.setText(customerList.get(position).getAddress;
      holder.customerphone.setText(customerList.get(position).getPhone;

}

And you can get the data like this:

FirebaseDatabase.getInstance().getReference().child("customers").addValueEventListener(newValueEventlistener{
@OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
                List<Customer> custoemrList = newArrayList<>();
                for (final DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    Customercustomer=newCustomer();
                    customer.setName(snapshot.child("name").getValue().toString(); 
...
...
customerList.add(customer);

                }

                customerAdapter= newcustomerAdapter(customerList, YourActivity.this);
                recyclerView.setAdapter(chatsAdapter);
            }

            @OverridepublicvoidonCancelled(DatabaseError databaseError) {

            }
        });
});

And in your Customer class you have to add getters and setters. Press Alt + Insert -> Getters and Setters -> Select All -> Enter

This should be it

Solution 2:

Change this line of code:

Queryquery= FirebaseDatabase.getInstance().getReference().child("customers").limitToLast(50)

with

Queryquery= FirebaseDatabase.getInstance().getReference()
    .child("customers")
    .orderByChild("name")
    .limitToLast(50);

Post a Comment for "Firebase Database Load Data Into Listview"