Skip to content

Commit 1a1060b

Browse files
authored
Merge pull request #680 from SUPERCILEX/patch-1
Fixup db README
2 parents 3d073ce + 7e13e61 commit 1a1060b

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

database/README.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# FirebaseUI Database
22

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)
44

55
To use the FirebaseUI to display Firebase data, we need a few things:
66

@@ -113,6 +113,25 @@ protected void onCreate(Bundle savedInstanceState) {
113113
}
114114
```
115115

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+
116135
### Create a custom ViewHolder
117136

118137
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:
122141
```java
123142
public static class ChatHolder extends RecyclerView.ViewHolder {
124143
private final TextView mNameField;
125-
private final TextView mTextField;
144+
private final TextView mMessageField;
126145

127146
public ChatHolder(View itemView) {
128147
super(itemView);
129148
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);
131150
}
132151

133152
public void setName(String name) {
134153
mNameField.setText(name);
135154
}
136155

137-
public void setText(String text) {
138-
mTextField.setText(text);
156+
public void setMessage(String message) {
157+
mMessageField.setText(message);
139158
}
140159
}
141160
```
142161

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-
162162
### Create custom FirebaseRecyclerAdapter subclass
163163

164164
Next, we need to create a subclass of the `FirebaseRecyclerAdapter` with the correct parameters
@@ -177,13 +177,13 @@ protected void onCreate(Bundle savedInstanceState) {
177177

178178
mAdapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(
179179
Chat.class,
180-
ChatHolder.class,
181180
R.layout.message,
181+
ChatHolder.class,
182182
ref) {
183183
@Override
184184
public void populateViewHolder(ChatHolder holder, Chat chat, int position) {
185185
holder.setName(chat.getName());
186-
holder.setText(chat.getMessage());
186+
holder.setMessage(chat.getMessage());
187187
}
188188
};
189189

@@ -235,13 +235,13 @@ protected void onCreate(Bundle savedInstanceState) {
235235

236236
mAdapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(
237237
Chat.class,
238-
ChatHolder.class,
239238
R.layout.message,
239+
ChatHolder.class,
240240
ref) {
241241
@Override
242242
public void populateViewHolder(ChatHolder holder, Chat chat, int position) {
243243
holder.setName(chat.getName());
244-
holder.setText(chat.getMessage());
244+
holder.setMessage(chat.getMessage());
245245
}
246246
};
247247

doc-images/chat-messages.png

136 KB
Loading

0 commit comments

Comments
 (0)