You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: codelabs/chat/README.md
+14-13Lines changed: 14 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -195,7 +195,7 @@ Next we'll send data to Firebase! In this step we'll allow the user to enter a m
195
195
mFirebaseRef.push().setValue(values);
196
196
textEdit.setText("");
197
197
}
198
-
});
198
+
});
199
199
200
200
You will have to import the packages for some of these classes. Android Studio will tell you where to import them from.
201
201
@@ -212,15 +212,15 @@ Now that we can send messages to Firebase, it is time for the next step: making
212
212
213
213
## Show the (existing and new) messages
214
214
215
-
A chat app that doesn’t show existing messages is not very useful. So in this step we’ll add a list of the existing messages to our Android app. At the end of this section we’ll have a fully functional chat app.
215
+
A chat app that doesn’t show existing messages is not very useful. So in this step we’ll add a list of the existing messages to our Android app. And since we're using Firebase, new chat messages will be added to this list automatically. At the end of this section we’ll have a fully functional chat app.
216
216
217
-
[screenshot:chat messages in list and new message]
217
+

218
218
219
219
Let's take this in chunks: first we'll create a Java class to represent each message, then we'll create an Adapter that gets each of the messages from Firebase and puts them into a ListView.
220
220
221
221
1. As you can see in the screenshot, each chat message has the same layout. Instead of creating a custom layout, we'll use one of the built-in layouts of Android: `android.R.layout.two_line_list_item`. This layout displays the items like this:
222
222
223
-
[screenshot:two_line_list_item]
223
+

224
224
225
225
We'll show the user name on the first line (in bold) and the message text on the second line.
226
226
2. Now create a class `ChatMessage.java` that wraps the username and text message:
@@ -248,37 +248,38 @@ Let's take this in chunks: first we'll create a Java class to represent each mes
248
248
249
249
As you can see, this is plain-old Java object. But it’s a POJO with some special traits. First `ChatMessage` follows a JavaBean pattern for its property names. The `getName` method is a getter for a `name` property, while `getText()` is a getter for a `text` property. And second, those property names correspond to the ones we’ve been using when we sent messages to Firebase in our `OnClickListener`.
250
250
251
-
[screenshot:ChatMessage.java]
251
+

252
252
253
253
Warning: if you end up making this `ChatMessage` an inner class of another class, you must make it static: `public static class ChatMessage`.
254
254
255
255
3. With the layout for the message specified and their structure defined in a class, we need to make a space for them in the `main_activity.xml`
256
256
257
-
4. Add a ListView with `android:id="@android:id/list"`` above the LinearLayout:
257
+
Add a ListView with `android:id="@android:id/list"`` above the LinearLayout:
258
258
259
259
<ListView
260
260
android:id="@android:id/list"
261
261
android:layout_width="match_parent"
262
262
android:layout_height="match_parent"
263
263
android:layout_above="@+id/footer"/>
264
264
265
-
This is the container that all messages will be added to: one message_layout for each ChatMessage.
265
+
This is the container that all messages will be added to: one message_layout for each ChatMessage.
266
266
267
-
[screenshot:activity_main.xml]
267
+

268
268
269
-
The `id` value is very important here, since Android's `ListActivity` uses it to find the `ListView`. So make sure to enter it exactly as specified: ``@android:id/list`.
269
+
The `id` value is very important here, since Android's `ListActivity` uses it to find the `ListView`. So make sure to enter it exactly as specified: ``@android:id/list`.
270
270
271
271
4. Make the `MainActivity` class descend from `ListActivity`. This is a built-in Android base-class. By deriving from this, our activity will automatically have access to the ListView we added to the layout:
272
272
273
273
public class MainActivity extends ListActivity {
274
274
275
-
[screenshot:MainActivity descends from ListActivity]
276
-
277
275
5. We're ready to start on our ListAdapter, which we'll base on the `FirebaseListAdapter` from the firebase-ui project we imported. `The FirebaseListAdapter` class adapts a Firebase collection so that it becomes usable in an Android `ListView`. First we'll add a member to our `MainActivity`:
6. To make everything come together, we add this to the onCreate method of our MainActivity:
283
284
284
285
mListAdapter = new FirebaseListAdapter<ChatMessage>(this, ChatMessage.class,
@@ -293,7 +294,7 @@ Let's take this in chunks: first we'll create a Java class to represent each mes
293
294
294
295
The FirebaseListAdapter maps the data from your Firebase database into the ListView that you added to the layout. It creates a new instance of your `two_line_list_item` for each `ChatMessage` and calls the `populateView method`. We override this method and put the name and text in the correct subviews.
295
296
296
-
[screenshot:MainActivity code]
297
+

297
298
298
299
7. Don't worry, the hardest part is behind us now. All that is left in this step is some clean-up. But before that, run your app and see that it shows all existing messages. And if you send a new message, it shows up in the emulator and in your Firebase dashboard.
299
300
@@ -317,7 +318,7 @@ Let's take this in chunks: first we'll create a Java class to represent each mes
317
318
mListAdapter.cleanup();
318
319
}
319
320
320
-
[screenshot: showing OnClickListener and onDestroy]
321
+

321
322
322
323
In this section we made our app show the chat messages. It was a lot of work, but in the end you can see that the Java code for our main activity still fits in a single screenshot.
0 commit comments