@@ -95,11 +95,127 @@ In the above snippet we have a query for the last 5 chat messages. Whenever thos
95
95
we get the ` ChatMessage ` objects from the ` DataSnapshot ` with ` getValue(ChatMessage.class) ` . The Firebase Android client will
96
96
then read the properties that it got from the database and map them to the fields of our ` ChatMessage ` class.
97
97
98
- But when we build our app using the ` FirebaseListAdapter ` , we often won't need to register our own EventListener. The
98
+ But when we build our app using FirebaseUI , we often won't need to register our own EventListener. The
99
99
` FirebaseListAdapter ` takes care of that for us.
100
100
101
101
### Subclassing the FirebaseListAdapter
102
102
103
+ #### Look up the ListView
103
104
105
+ We'll assume you've already added a ` ListView ` to your layout and have looked it up in the ` onCreate ` method of your activity:
106
+
107
+ @Override
108
+ protected void onCreate(Bundle savedInstanceState) {
109
+ super.onCreate(savedInstanceState);
110
+ setContentView(R.layout.activity_main);
111
+
112
+ ListView messagesView = (ListView) findViewById(R.id.messages_list);
113
+ }
114
+
115
+ #### Set up connection to Firebase
116
+
117
+ First we'll tell Firebase that we intend to use it in this activity and set up a reference to the database of chat message.
118
+
119
+ @Override
120
+ protected void onCreate(Bundle savedInstanceState) {
121
+ super.onCreate(savedInstanceState);
122
+ setContentView(R.layout.activity_main);
123
+
124
+ ListView messagesView = (ListView) findViewById(R.id.messages_list);
125
+
126
+ Firebase.setAndroidContext(this);
127
+ Firebase ref = new Firebase("https://nanochat.firebaseio.com");
128
+ }
129
+
130
+ #### Create custom FirebaseListAdapter subclass
131
+
132
+ Next, we need to create a subclass of the ` FirebaseListAdapter ` with the correct parameters and implement its ` populateView ` method:
133
+
134
+ @Override
135
+ protected void onCreate(Bundle savedInstanceState) {
136
+ super.onCreate(savedInstanceState);
137
+ setContentView(R.layout.activity_main);
138
+
139
+ ListView messagesView = (ListView) findViewById(R.id.messages_list);
140
+
141
+ Firebase.setAndroidContext(this);
142
+ Firebase ref = new Firebase("https://nanochat.firebaseio.com");
143
+
144
+ mAdapter = new FirebaseListAdapter<ChatMessage>(ChatMessage.class, android.R.layout.two_line_list_item, this, ref) {
145
+ @Override
146
+ protected void populateView(View view, ChatMessage chatMessage) {
147
+ ((TextView)view.findViewById(android.R.id.text1)).setText(chatMessage.getName());
148
+ ((TextView)view.findViewById(android.R.id.text2)).setText(chatMessage.getMessage());
149
+
150
+ }
151
+ };
152
+ messagesView.setListAdapter(mAdapter);
153
+ }
154
+
155
+ In this last snippet we create a subclass of ` FirebaseListAdapter ` .
156
+ We tell is that it is of type ` <ChatMessage> ` , so that it is a type-safe collection. We also tell it to use
157
+ ` ChatMessage.class ` when reading messages from the database. Next we say that each message will be displayed in
158
+ a ` android.R.layout.two_line_list_item ` , which is a built-in layout in Android that has two ` TextView ` elements
159
+ under each other. Then we say that the adapter belongs to ` this ` activity and that it needs to monitor the
160
+ data location in ` ref ` .
161
+
162
+ We also have to override the ` populateView() ` method, which is abstract in the ` FirebaseListAdapter ` . The
163
+ ` FirebaseListAdapter ` will call our ` populateView ` method for each ` ChatMessage ` it finds in the database.
164
+ It passes us the ` ChatMessage ` and a ` View ` , which is an instance of the ` android.R.layout.two_line_list_item `
165
+ we specified in the constructor. So what we do in our subclass is map the fields from ` chatMessage ` to the
166
+ correct ` TextView ` controls from the ` view ` . The code is a bit verbose, but hey... that's Java and Android for you.
167
+
168
+ #### Clean up when the activity is destroyed
169
+
170
+ Finally, we need to clean up after ourselves. When the activity is destroyed, we need to call ` release() `
171
+ on the ` ListAdapter ` so that it can stop listening for changes in the Firebase database.
172
+
173
+ @Override
174
+ protected void onDestroy() {
175
+ super.onDestroy();
176
+ mAdapter.cleanup();
177
+ }
178
+
179
+ #### Sending chat messages
180
+
181
+ Remember when we showed how to use the ` ChatMessage ` class in ` setValue() ` .
182
+ We can now use that in our activity to allow sending a message:
183
+
184
+ @Override
185
+ protected void onCreate(Bundle savedInstanceState) {
186
+ super.onCreate(savedInstanceState);
187
+ setContentView(R.layout.activity_main);
188
+
189
+ ListView messagesView = (ListView) findViewById(R.id.messages_list);
190
+
191
+ Firebase.setAndroidContext(this);
192
+ Firebase ref = new Firebase("https://nanochat.firebaseio.com");
193
+
194
+ mAdapter = new FirebaseListAdapter<ChatMessage>(ChatMessage.class, android.R.layout.two_line_list_item, this, ref) {
195
+ @Override
196
+ protected void populateView(View view, ChatMessage chatMessage) {
197
+ ((TextView)view.findViewById(android.R.id.text1)).setText(chatMessage.getName());
198
+ ((TextView)view.findViewById(android.R.id.text2)).setText(chatMessage.getMessage());
199
+ }
200
+ };
201
+ setListAdapter(mAdapter);
202
+
203
+ final EditText mMessage = (EditText) findViewById(R.id.message_text);
204
+ findViewById(R.id.send_button).setOnClickListener(new View.OnClickListener() {
205
+ @Override
206
+ public void onClick(View v) {
207
+ mRef.push().setValue(new ChatMessage("puf", mMessage.getText().toString()));
208
+ mMessage.setText("");
209
+ }
210
+ });
211
+ }
212
+
213
+ @Override
214
+ protected void onDestroy() {
215
+ super.onDestroy();
216
+ mAdapter.cleanup();
217
+ }
218
+
219
+ Et voila: a minimal, yet fully functional, chat app in about 30 lines of code. Not bad, right?
104
220
105
221
## Contributing to the library
0 commit comments