Firebase database load data into Listview

為{幸葍}努か 提交于 2020-01-16 09:03:09

问题


I have this DB structure:

{
    "customers" : {
       "-L-OcgJ0kwTNSm6HoSvG" : {
         "address" : "Test Alamat",
         "birthday" : "1990-12-03",
         "email" : "Dodi@gmail.com",
         "name" : "Dodi",
         "outletID" : "2673",
         "phone" : "09888777111"
       }
    }
}

Now i want to load all data of "customers" into ListView using FirebaseUI-Android library. And here is the codes:

Query query = FirebaseDatabase.getInstance().getReference().child("customers").limitToLast(50);

FirebaseListOptions<Customers> options = new FirebaseListOptions.Builder<Customers>()
                    .setLayout(R.layout.row_customer)
                    .setQuery(query, Customers.class)
                    .build();

FirebaseListAdapter<Customers> adapter = new FirebaseListAdapter<Customers>(options) {
                @Override
                protected void populateView(View view, Customers customer, int position) {
                    ((TextView) view.findViewById(R.id.txtCustomerName)).setText(customer.name);
                    ((TextView) view.findViewById(R.id.txtCustomerAddress)).setText(customer.address);
                    ((TextView) view.findViewById(R.id.txtCustomerPhone)).setText(customer.phone);



  //and i've set the adapter into ListView
  ((ListView)layout.findViewById(R.id.lvCustomerList)).setAdapter(adapter);

And here is Customers.java:

@IgnoreExtraProperties
public class Customers {
    public String name, outletID, address, phone, birthday, email;

    public Customers() {
    }

    public Customers(String name, String outletID, String address, String phone, String birthday, String email) {
       this.name = name;
       this.outletID = outletID;
       this.address = address;
       this.phone = phone;
       this.birthday = birthday;
       this.email = email;
    }
}

Please help me what is the problem with my source code? i've run it and the data failed to display (only blank on my listview). There's no errors on my Android Studio logs.


回答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:

public class CustomerAdapter extends RecyclerView.Adapter<CustomerAdapter.MessageViewHolder> {
private List<Customer> customerList;                                                                        
private Context context;



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

@Override
public CustomerAdapter.MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
   View v = LayoutInflater.from(parent.getContext())
                   .inflate(R.layout.your_layout, parent, false);
         return new CustomerAdapter.MessageViewHolder(v);
} 
public class CustomerViewHolder extends RecyclerView.ViewHolder {
     public TextView customername, customeraddress, customerphone;

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

@Override
public int getItemCount() {
    return customerList.size();
}

@Override
public void onBindViewHolder(final CustomerAdapter.MessageViewHolder holder, final int 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(new ValueEventlistener{
@Override
         public void onDataChange(DataSnapshot dataSnapshot) {
                List<Customer> custoemrList = new ArrayList<>();
                for (final DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    Customer customer = new Customer();
                    customer.setName(snapshot.child("name").getValue().toString(); 
...
...
customerList.add(customer);

                }

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

            @Override
            public void onCancelled(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




回答2:


Change this line of code:

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

with

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


来源:https://stackoverflow.com/questions/47614320/firebase-database-load-data-into-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!