2017-12-03 64 views
0

我有這個DB結構:火力地堡數據庫將數據加載到列表視圖

{ 
    "customers" : { 
     "-L-OcgJ0kwTNSm6HoSvG" : { 
     "address" : "Test Alamat", 
     "birthday" : "1990-12-03", 
     "email" : "[email protected]", 
     "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日誌沒有錯誤。

回答

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

      } 
     }); 
}); 

而在您的客戶類中,您必須添加獲取者和設置者。 按Alt +插入 - > getter和setter - >全選 - >輸入

這應該是它

+0

感謝,我會將其更改爲recyclerView。 – wdyz

0

更改這行代碼:

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

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