我遵循本指南:
https://firebase.google.com/support/guides/firebase-android
我已经升级了除FirebaseListAdapter
命令之外的所有代码。
有没有简单的替代方案?我无法单独使用它,因为最后一个输入参数是Firebase
,而且不再可用。
以下是我的代码:
adapter = new FirebaseListAdapter<myObject>(this, myObject.class, R.layout.mylist, mRef) {
@Override
protected void populateView(View view, myObject currentOb, int i) {
...
}
但mRef
根据最后一次输入的需要设置为DatabaseReference
而不是Firebase ref
..
非常感谢。
答案 0 :(得分:1)
您需要将FirebaseUI库升级到与您正在使用的Firebase SDK兼容的版本。
FirebaseUI readme包含这个方便的表格:
FirebaseUI Firebase/Play
Version Services Version
0.6.0 9.6.0
0.5.3 9.4.0
0.4.4 9.4.0
0.4.3 9.2.1
0.4.2 9.2.0
0.4.1 9.0.2
0.4.0 9.0.0
答案 1 :(得分:0)
您可以使用标准的RecyclerView。
例如:
持有人:
private static class CommentViewHolder extends RecyclerView.ViewHolder {
public TextView authorView;
public TextView bodyView;
public CommentViewHolder(View itemView) {
super(itemView);
authorView = (TextView) itemView.findViewById(R.id.comment_author);
bodyView = (TextView) itemView.findViewById(R.id.comment_body);
}
}
适配器:
private static class CommentAdapter extends RecyclerView.Adapter<CommentViewHolder> {
private Context mContext;
private DatabaseReference mDatabaseReference;
private ChildEventListener mChildEventListener;
private List<String> mCommentIds = new ArrayList<>();
private List<GoogleExample_Comment> mGoogleExampleComments = new ArrayList<>();
public CommentAdapter(final Context context, DatabaseReference ref) {
mContext = context;
mDatabaseReference = ref;
// Create child event listener
// [START child_event_listener_recycler]
ChildEventListener childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());
// A new googleExampleComment has been added, add it to the displayed list
GoogleExample_Comment googleExampleComment = dataSnapshot.getValue(GoogleExample_Comment.class);
// [START_EXCLUDE]
// Update RecyclerView
mCommentIds.add(dataSnapshot.getKey());
mGoogleExampleComments.add(googleExampleComment);
notifyItemInserted(mGoogleExampleComments.size() - 1);
// [END_EXCLUDE]
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildChanged:" + dataSnapshot.getKey());
// A comment has changed, use the key to determine if we are displaying this
// comment and if so displayed the changed comment.
GoogleExample_Comment newGoogleExampleComment = dataSnapshot.getValue(GoogleExample_Comment.class);
String commentKey = dataSnapshot.getKey();
// [START_EXCLUDE]
int commentIndex = mCommentIds.indexOf(commentKey);
if (commentIndex > -1) {
// Replace with the new data
mGoogleExampleComments.set(commentIndex, newGoogleExampleComment);
// Update the RecyclerView
notifyItemChanged(commentIndex);
} else {
Log.w(TAG, "onChildChanged:unknown_child:" + commentKey);
}
// [END_EXCLUDE]
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Log.d(TAG, "onChildRemoved:" + dataSnapshot.getKey());
// A comment has changed, use the key to determine if we are displaying this
// comment and if so remove it.
String commentKey = dataSnapshot.getKey();
// [START_EXCLUDE]
int commentIndex = mCommentIds.indexOf(commentKey);
if (commentIndex > -1) {
// Remove data from the list
mCommentIds.remove(commentIndex);
mGoogleExampleComments.remove(commentIndex);
// Update the RecyclerView
notifyItemRemoved(commentIndex);
} else {
Log.w(TAG, "onChildRemoved:unknown_child:" + commentKey);
}
// [END_EXCLUDE]
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildMoved:" + dataSnapshot.getKey());
// A comment has changed position, use the key to determine if we are
// displaying this comment and if so move it.
GoogleExample_Comment movedGoogleExampleComment = dataSnapshot.getValue(GoogleExample_Comment.class);
String commentKey = dataSnapshot.getKey();
// ...
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "postComments:onCancelled", databaseError.toException());
Toast.makeText(mContext, "Failed to load comments.",
Toast.LENGTH_SHORT).show();
}
};
ref.addChildEventListener(childEventListener);
// [END child_event_listener_recycler]
// Store reference to listener so it can be removed on app stop
mChildEventListener = childEventListener;
}
@Override
public CommentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.google_example_item_comment, parent, false);
return new CommentViewHolder(view);
}
@Override
public void onBindViewHolder(CommentViewHolder holder, int position) {
GoogleExample_Comment googleExampleComment = mGoogleExampleComments.get(position);
holder.authorView.setText(googleExampleComment.author);
holder.bodyView.setText(googleExampleComment.text);
}
@Override
public int getItemCount() {
return mGoogleExampleComments.size();
}
public void cleanupListener() {
if (mChildEventListener != null) {
mDatabaseReference.removeEventListener(mChildEventListener);
}
}
}
希望它对你有所帮助!