Firebase数据库将数据加载到Listview中

时间:2017-12-03 01:14:18

标签: android listview firebase-realtime-database loaddata

我有这个数据库结构:

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

现在我想加载"客户"的所有数据使用FirebaseUI-Android库进入ListView。以下是代码:

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

这是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;
    }
}

请帮我解决一下我的源代码有什么问题? 我运行它并且数据无法显示(我的列表视图中只有空白)。我的Android Studio日志没有错误。

2 个答案:

答案 0 :(得分:1)

我建议您创建自定义适配器并使用RecyclerView(它比ListView更快更好)

这样的事情:

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;

}

你可以得到这样的数据:

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

            }
        });
});

在您的Customer类中,您必须添加getter和setter。 按Alt + Insert - &gt;吸气剂和二传手 - &gt;全选 - &gt;输入

这应该是

答案 1 :(得分:0)

更改此行代码:

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

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