Skip to content

Commit ca5af10

Browse files
committed
Added section on creating a model class
1 parent a2dd06d commit ca5af10

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,75 @@ After the project is synchronized, we're ready to start using Firebase functiona
3131

3232
### Creating a model class
3333

34+
In your app, create a class that represents the data from Firebase that you want to show in the ListView.
3435

36+
So say we have these chat messages in our Firebase database:
37+
38+
![Chat messages in dashboard](doc-images/5-chat-messages-in-dashboard.png "Chat messages in dashboard")
39+
40+
We can represent a chat message with this Java class:
41+
42+
public class ChatMessage {
43+
String message;
44+
String name;
45+
46+
public ChatMessage() {
47+
}
48+
49+
public ChatMessage(String name, String message) {
50+
this.message = message;
51+
this.name = name;
52+
}
53+
54+
public String getMessage() {
55+
return message;
56+
}
57+
58+
public String getName() {
59+
return name;
60+
}
61+
}
62+
63+
A few things to note here:
64+
65+
* the field have the exact same name as the properties in Firebase. This allows Firebase to automatically map the properties to these fields.
66+
* there is a default (parameterless constructor) that is necessary for Firebase to be able to create a new instance of this class
67+
* there is a convenience constructor that takes the member fields, so that we easily create a fully initialized `ChatMessage` in our app
68+
* the `getMessage` and `getName` methods are so-called getters and follow a JavaBean pattern
69+
70+
A little-known feature of Firebase for Android is that you can pass an instance of this `ChatMessage` class to `setValue()`:
71+
72+
Firebase ref = new Firebase("https://nanochat.firebaseio.com/");
73+
ChatMessage msg = new ChatMessage("puf", "Hello FirebaseUI world!");
74+
ref.push().setValue(msg);
75+
76+
The Firebase Android client will read the values from the `msg` and write them into the properties of the new child in the database.
77+
78+
Conversely, we can read a `ChatMessage` straight from a `DataSnapshot` in our event handlers:
79+
80+
ref.limitToLast(5).addValueEventListener(new ValueEventListener() {
81+
@Override
82+
public void onDataChange(DataSnapshot snapshot) {
83+
for (DataSnapshot msgSnapshot: snapshot.getChildren()) {
84+
ChatMessage msg = msgSnapshot.getValue(ChatMessage.class);
85+
Log.i("Chat", chat.getName()+": "+chat.getMessage());
86+
}
87+
}
88+
@Override
89+
public void onCancelled(FirebaseError firebaseError) {
90+
Log.e("Chat", "The read failed: " + firebaseError.getMessage());
91+
}
92+
});
93+
94+
In the above snippet we have a query for the last 5 chat messages. Whenever those change (i.e. when an new message is added)
95+
we get the `ChatMessage` objects from the `DataSnapshot` with `getValue(ChatMessage.class)`. The Firebase Android client will
96+
then read the properties that it got from the database and map them to the fields of our `ChatMessage` class.
97+
98+
But when we build our app using the `FirebaseListAdapter`, we often won't need to register our own EventListener. The
99+
`FirebaseListAdapter` takes care of that for us.
35100

36101
### Subclassing the FirebaseListAdapter
37102

38103

104+
39105
## Contributing to the library

library/src/main/java/com/firebase/ui/FirebaseRecyclerViewAdapter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import com.firebase.client.ChildEventListener;
77
import com.firebase.client.DataSnapshot;
8+
import com.firebase.client.Firebase;
89
import com.firebase.client.FirebaseError;
910
import com.firebase.client.Query;
1011

@@ -36,7 +37,6 @@ public FirebaseRecyclerViewAdapter(Query ref, Class<T> modelClass) {
3637
mModelClass = modelClass;
3738
mSnapshots = new FirebaseArray(ref);
3839

39-
// TODO: implement separate notifications for added, removed, changed and moved
4040
mSnapshots.setOnChangedListener(new FirebaseArray.OnChangedListener() {
4141
@Override
4242
public void onChanged(EventType type, int index, int oldIndex) {
@@ -74,6 +74,8 @@ public T getItem(int position) {
7474
return mSnapshots.getItem(position).getValue(mModelClass);
7575
}
7676

77+
public Firebase getRef(int position) { return mSnapshots.getItem(position).getRef(); }
78+
7779
@Override
7880
public long getItemId(int position) {
7981
// http://stackoverflow.com/questions/5100071/whats-the-purpose-of-item-ids-in-android-listview-adapter

0 commit comments

Comments
 (0)