回答

1

您可以使用FirebaseUI for Android庫。

只需添加依賴關係(在0.5.3工程與火力9.4.0庫):

dependencies { 
    // Single target that includes all FirebaseUI libraries 
    compile 'com.firebaseui:firebase-ui:0.5.3' 
} 

定義DatabaseReference

FirebaseDatabase database = FirebaseDatabase.getInstance(); 
DatabaseReference ref = database.getReference("passengers"); 

然後:

  • 創建定製ViewHolder
  • Creat EA自定義子類FirebaseRecyclerAdapter

ViewHolder類:

public static class PassengerHolder extends RecyclerView.ViewHolder { 

} 

FirebaseRecyclerAdapter類:

RecyclerView recycler = (RecyclerView) findViewById(R.id.messages_recycler); 
recycler.setHasFixedSize(true); 
recycler.setLayoutManager(new LinearLayoutManager(this)); 

mAdapter = new FirebaseRecyclerAdapter<Passenger, PassengerHolder>(Passenger.class, R.layout.mylayout, PassengerHolder.class, mRef) { 
    @Override 
    public void populateViewHolder(PassengerHolder passengerViewHolder, Passenger passenger, int position) { 
     passengerViewHolder.setName(passenger.getName()); 
     //....... 

    } 
}; 
recycler.setAdapter(mAdapter); 
+0

我也想用'CardView'先生。這怎麼可能? – tin

+0

@tin在您的佈局文件(由R.layout.mylayout引用)中只包含CardView。 –

+0

嗨,@GabrieleMariotti我與我的應用程序有同樣的問題。我似乎無法讀取所有數據,而是我的應用程序只從第一個節點/用戶讀取數據。請你幫忙,我試着你在這裏做什麼,但我的結構有點不同,我似乎無法得到它的工作:http://stackoverflow.com/questions/41797009/using-search-view-android- with-firebase – SumOne

1

你可以嘗試這樣的:

 FirebaseDatabase database = FirebaseDatabase.getInstance(); 
     DatabaseReference ref = database.getReference(); 

     ref.addListenerForSingleValueEvent(new ValueEventListener() { 
       @Override 
       public void onDataChange(DataSnapshot dataSnapshot) { 
        //Your Logic here 
        for (DataSnapshot eventSnapshot : dataSnapshot.getChildren()) { 
        YourModel mModel = eventSnapshot.getValue(YourModel.class); 
        Log.e("DATA" ,""+ mModel); 
        } 
       } 

       @Override 
       public void onCancelled(DatabaseError databaseError) { 

       } 
      }); 

你必須創建一個ARRA您RecycleviewŸ列表並添加每個模型即mModel您ArrayList和顯示在列表中

More Info 1

+0

感謝rashid它爲我工作.. –