@@ -31,9 +31,75 @@ After the project is synchronized, we're ready to start using Firebase functiona
31
31
32
32
### Creating a model class
33
33
34
+ In your app, create a class that represents the data from Firebase that you want to show in the ListView.
34
35
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.
35
100
36
101
### Subclassing the FirebaseListAdapter
37
102
38
103
104
+
39
105
## Contributing to the library
0 commit comments