简体   繁体   中英

How to upgrade FirebaseListAdapter

I'm following this guide:

https://firebase.google.com/support/guides/firebase-android

I've already upgraded all of my code except the command FirebaseListAdapter .

Is there any simple alternative to it? I can't use it on its own because the last input parameter is Firebase , and thats no longer available.

Here is my code that I'm stuck with:

adapter = new FirebaseListAdapter<myObject>(this, myObject.class, R.layout.mylist, mRef) {
            @Override
            protected void populateView(View view, myObject currentOb, int i) {

            ...

}

but the mRef is setup as DatabaseReference and not Firebase ref as needed for the last input..

Thanks ahead.

You'll need to upgrade the FirebaseUI library to a version that is compatible with the Firebase SDK that you're using.

The FirebaseUI readme contains this handy table:

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

You may using standart RecyclerView.

For example:

Holder:

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);
        }
    }

Adapter:

    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);
            }
        }

    }

Hope it helps for you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM