1
1
# FirebaseUI Database
2
2
3
- ## Using FirebaseUI to populate a ` RecyclerView `
3
+ ## Using FirebaseUI to populate a [ ` RecyclerView ` ] ( https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html )
4
4
5
5
To use the FirebaseUI to display Firebase data, we need a few things:
6
6
@@ -113,6 +113,25 @@ protected void onCreate(Bundle savedInstanceState) {
113
113
}
114
114
```
115
115
116
+ There's nothing magical going on here; we're just mapping numeric IDs and casts into a nice, type-safe contract.
117
+
118
+ ### Connect to Firebase
119
+
120
+ First we'll set up a reference to the database of chat messages:
121
+
122
+ ``` java
123
+ @Override
124
+ protected void onCreate(Bundle savedInstanceState) {
125
+ super . onCreate(savedInstanceState);
126
+ setContentView(R . layout. activity_main);
127
+
128
+ RecyclerView messages = (RecyclerView ) findViewById(R . id. messages);
129
+ messages. setLayoutManager(new LinearLayoutManager (this ));
130
+
131
+ DatabaseReference ref = FirebaseDatabase . getInstance(). getReference();
132
+ }
133
+ ```
134
+
116
135
### Create a custom ViewHolder
117
136
118
137
A ViewHolder is similar to container of a ViewGroup that allows simple lookup of the sub-views of the group.
@@ -122,43 +141,24 @@ We can wrap that in a ViewHolder with:
122
141
``` java
123
142
public static class ChatHolder extends RecyclerView .ViewHolder {
124
143
private final TextView mNameField;
125
- private final TextView mTextField ;
144
+ private final TextView mMessageField ;
126
145
127
146
public ChatHolder (View itemView ) {
128
147
super (itemView);
129
148
mNameField = (TextView ) itemView. findViewById(android. R . id. text1);
130
- mTextField = (TextView ) itemView. findViewById(android. R . id. text2);
149
+ mMessageField = (TextView ) itemView. findViewById(android. R . id. text2);
131
150
}
132
151
133
152
public void setName (String name ) {
134
153
mNameField. setText(name);
135
154
}
136
155
137
- public void setText (String text ) {
138
- mTextField . setText(text );
156
+ public void setMessage (String message ) {
157
+ mMessageField . setText(message );
139
158
}
140
159
}
141
160
```
142
161
143
- There's nothing magical going on here; we're just mapping numeric IDs and casts into a nice, type-safe contract.
144
-
145
- ### Connect to Firebase
146
-
147
- First we'll set up a reference to the database of chat messages:
148
-
149
- ``` java
150
- @Override
151
- protected void onCreate(Bundle savedInstanceState) {
152
- super . onCreate(savedInstanceState);
153
- setContentView(R . layout. activity_main);
154
-
155
- RecyclerView messages = (RecyclerView ) findViewById(R . id. messages);
156
- messages. setLayoutManager(new LinearLayoutManager (this ));
157
-
158
- DatabaseReference ref = FirebaseDatabase . getInstance(). getReference();
159
- }
160
- ```
161
-
162
162
### Create custom FirebaseRecyclerAdapter subclass
163
163
164
164
Next, we need to create a subclass of the ` FirebaseRecyclerAdapter ` with the correct parameters
@@ -177,13 +177,13 @@ protected void onCreate(Bundle savedInstanceState) {
177
177
178
178
mAdapter = new FirebaseRecyclerAdapter<Chat , ChatHolder > (
179
179
Chat . class,
180
- ChatHolder . class,
181
180
R . layout. message,
181
+ ChatHolder . class,
182
182
ref) {
183
183
@Override
184
184
public void populateViewHolder (ChatHolder holder , Chat chat , int position ) {
185
185
holder. setName(chat. getName());
186
- holder. setText (chat. getMessage());
186
+ holder. setMessage (chat. getMessage());
187
187
}
188
188
};
189
189
@@ -235,13 +235,13 @@ protected void onCreate(Bundle savedInstanceState) {
235
235
236
236
mAdapter = new FirebaseRecyclerAdapter<Chat , ChatHolder > (
237
237
Chat . class,
238
- ChatHolder . class,
239
238
R . layout. message,
239
+ ChatHolder . class,
240
240
ref) {
241
241
@Override
242
242
public void populateViewHolder (ChatHolder holder , Chat chat , int position ) {
243
243
holder. setName(chat. getName());
244
- holder. setText (chat. getMessage());
244
+ holder. setMessage (chat. getMessage());
245
245
}
246
246
};
247
247
0 commit comments