简体   繁体   中英

Can't make viewholder final after firebase UI update

I updated my com.firebaseui:firebase-ui-database package from 1.1.1 to 3.3.1. It's seems that I can't use populateViewHolder anymore in the FirebaseRecyclerAdapter . Instead I should use onBindViewHolder . Which I assume is fine, but when I call the viewholder from within a inner class (another database Ref) I need to make the viewholder final, at that point the adapter gives the following error:

在此处输入图片说明

The code

addedFriendsRecycleViewAdapter = new FirebaseRecyclerAdapter<Boolean, SingleImageViewHolder>(
      Boolean.class,
      R.layout.adduser_single_round_pictrure_layout,
      SingleImageViewHolder.class,
      mMembersDatabase
) {
      @Override
      public SingleImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          return null;
      }

      @Override
      protected void onBindViewHolder(@NonNull final SingleImageViewHolder holder, int position, @NonNull Boolean model) {
          final String added_list_user_id = getRef(position).getKey();

          mUsersDatabase.child(added_list_user_id).addValueEventListener(new ValueEventListener() {
              @Override
              public void onDataChange(DataSnapshot dataSnapshot) {

                  String userThumb = dataSnapshot.child("thumb_image").getValue().toString();

                  holder.setImage(userThumb, AddUserActivity.this);

              }

              @Override
              public void onCancelled(DatabaseError databaseError) {

              }
          });
      }
  };

You need to use FirebaseRecyclerOptions in version 3.0+:

First, configure the adapter by building FirebaseRecyclerOptions:

 FirebaseRecyclerOptions<POJO> options =
            new FirebaseRecyclerOptions.Builder<POJO>()
                    .setQuery(query, POJO.class)
                    .build();

Next create the FirebaseRecyclerAdapter object:

FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<POJO, SingleImageViewHolder>(options) {
@Override
public SingleImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // Create a new instance of the ViewHolder, in this case we are using a custom
    // layout called R.layout.message for each item
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.message, parent, false);

    return new SingleImageViewHolder(view);
}

@Override
protected void onBindViewHolder(SingleImageViewHolder holder, int position, POJO model) {
    // Bind the Chat object to the ChatHolder
    // ...
   }
};

more details here:

https://github.com/firebase/FirebaseUI-Android/tree/master/database

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