2016-10-07 70 views
1

我这个指南:如何升级FirebaseListAdapter

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 ..提前

感谢。

回答

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 
+0

酷很容易解决!谢谢! 奇怪的是,它并没有提到有关更新firebaseUI依赖项的警告,因为它在其他依赖项中发出警告。 –

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

    } 

希望它可以帮助你!

+0

谢谢你呢! :) –