Skip to content

Commit b054219

Browse files
committed
Allow using DataSnapshot as model class, remove old populate method (without position parameter)
Fixes #9
1 parent 4b0bdf2 commit b054219

File tree

2 files changed

+21
-46
lines changed

2 files changed

+21
-46
lines changed

library/src/main/java/com/firebase/ui/FirebaseListAdapter.java

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import android.view.ViewGroup;
3434
import android.widget.BaseAdapter;
3535

36+
import com.firebase.client.DataSnapshot;
3637
import com.firebase.client.Firebase;
3738
import com.firebase.client.Query;
3839

@@ -48,7 +49,7 @@
4849
* Firebase ref = new Firebase("https://<yourapp>.firebaseio.com");
4950
* ListAdapter adapter = new FirebaseListAdapter<ChatMessage>(this, ChatMessage.class, android.R.layout.two_line_list_item, mRef)
5051
* {
51-
* protected void populateView(View view, ChatMessage chatMessage)
52+
* protected void populateView(View view, ChatMessage chatMessage, int position)
5253
* {
5354
* ((TextView)view.findViewById(android.R.id.text1)).setText(chatMessage.getName());
5455
* ((TextView)view.findViewById(android.R.id.text2)).setText(chatMessage.getMessage());
@@ -111,7 +112,14 @@ public int getCount() {
111112
}
112113

113114
@Override
114-
public T getItem(int i) { return mSnapshots.getItem(i).getValue(mModelClass); }
115+
public T getItem(int position) {
116+
if (mModelClass != DataSnapshot.class) {
117+
return mSnapshots.getItem(position).getValue(mModelClass);
118+
}
119+
else {
120+
return (T) mSnapshots.getItem(position);
121+
}
122+
}
115123

116124
public Firebase getRef(int position) { return mSnapshots.getItem(position).getRef(); }
117125

@@ -127,7 +135,7 @@ public View getView(int position, View view, ViewGroup viewGroup) {
127135
view = mActivity.getLayoutInflater().inflate(mLayout, viewGroup, false);
128136
}
129137

130-
T model = mSnapshots.getItem(position).getValue(mModelClass);
138+
T model = getItem(position);
131139

132140
// Call out to subclass to marshall this model into the provided view
133141
populateView(view, model, position);
@@ -140,30 +148,11 @@ public View getView(int position, View view, ViewGroup viewGroup) {
140148
* this class. The third argument is the item's position in the list.
141149
* <p>
142150
* Your implementation should populate the view using the data contained in the model.
143-
* You should implement either this method or the other {@link FirebaseListAdapter#populateView(View, Object)} method
144-
* but not both.
145151
*
146152
* @param v The view to populate
147153
* @param model The object containing the data used to populate the view
148154
* @param position The position in the list of the view being populated
149155
*/
150-
protected void populateView(View v, T model, int position) {
151-
populateView(v, model);
152-
}
156+
abstract protected void populateView(View v, T model, int position);
153157

154-
/**
155-
* This is a backwards compatible version of populateView.
156-
* <p>
157-
* You should implement either this method or the other {@link FirebaseListAdapter#populateView(View, Object, int)} method
158-
* but not both.
159-
*
160-
* @see FirebaseListAdapter#populateView(View, Object, int)
161-
*
162-
* @param v The view to populate
163-
* @param model The object containing the data used to populate the view
164-
*/
165-
@Deprecated
166-
protected void populateView(View v, T model) {
167-
168-
}
169158
}

library/src/main/java/com/firebase/ui/FirebaseRecyclerAdapter.java

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import android.view.View;
3434
import android.view.ViewGroup;
3535

36+
import com.firebase.client.DataSnapshot;
3637
import com.firebase.client.Firebase;
3738
import com.firebase.client.Query;
3839

@@ -68,7 +69,7 @@
6869
* recycler.setLayoutManager(new LinearLayoutManager(this));
6970
*
7071
* adapter = new FirebaseRecyclerAdapter<ChatMessage, ChatMessageViewHolder>(ChatMessage.class, android.R.layout.two_line_list_item, ChatMessageViewHolder.class, mRef) {
71-
* public void populateViewHolder(ChatMessageViewHolder chatMessageViewHolder, ChatMessage chatMessage) {
72+
* public void populateViewHolder(ChatMessageViewHolder chatMessageViewHolder, ChatMessage chatMessage, int position) {
7273
* chatMessageViewHolder.nameText.setText(chatMessage.getName());
7374
* chatMessageViewHolder.messageText.setText(chatMessage.getMessage());
7475
* }
@@ -136,7 +137,6 @@ public FirebaseRecyclerAdapter(Class<T> modelClass, int modelLayout, Class<VH> v
136137
this(modelClass, modelLayout, viewHolderClass, (Query) ref);
137138
}
138139

139-
140140
public void cleanup() {
141141
mSnapshots.cleanup();
142142
}
@@ -147,7 +147,12 @@ public int getItemCount() {
147147
}
148148

149149
public T getItem(int position) {
150-
return mSnapshots.getItem(position).getValue(mModelClass);
150+
if (mModelClass != DataSnapshot.class) {
151+
return mSnapshots.getItem(position).getValue(mModelClass);
152+
}
153+
else {
154+
return (T) mSnapshots.getItem(position);
155+
}
151156
}
152157

153158
public Firebase getRef(int position) { return mSnapshots.getItem(position).getRef(); }
@@ -186,29 +191,10 @@ public void onBindViewHolder(VH viewHolder, int position) {
186191
* this class. The third argument is the item's position in the list.
187192
* <p>
188193
* Your implementation should populate the view using the data contained in the model.
189-
* You should implement either this method or the other FirebaseRecyclerAdapter#populateViewHolder(VH, Object) method
190-
* but not both.
191194
*
192195
* @param viewHolder The view to populate
193196
* @param model The object containing the data used to populate the view
194197
* @param position The position in the list of the view being populated
195198
*/
196-
protected void populateViewHolder(VH viewHolder, T model, int position) {
197-
populateViewHolder(viewHolder, model);
198-
};
199-
/**
200-
* This is a backwards compatible version of populateViewHolder.
201-
* <p>
202-
* You should implement either this method or the other FirebaseRecyclerAdapter#populateViewHolder(VH, T, int) method
203-
* but not both.
204-
*
205-
* @see FirebaseListAdapter#populateView(View, Object, int)
206-
*
207-
* @param viewHolder The view to populate
208-
* @param model The object containing the data used to populate the view
209-
*/
210-
@Deprecated
211-
protected void populateViewHolder(VH viewHolder, T model) {
212-
};
213-
199+
abstract protected void populateViewHolder(VH viewHolder, T model, int position);
214200
}

0 commit comments

Comments
 (0)