我将com.firebaseui:firebase-ui-database
软件包从1.1.1更新为3.3.1。看来我不能在FirebaseRecyclerAdapter
中使用populateViewHolder了。相反,我应该使用onBindViewHolder
。我认为这很好,但是当我从内部类(另一个数据库Ref)中调用viewholder时,我需要使viewholder最终化,这时适配器会给出以下错误:
编码
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) {
}
});
}
};
最佳答案
您需要在3.0+版本中使用FirebaseRecyclerOptions
:
首先,通过构建FirebaseRecyclerOptions配置适配器:
FirebaseRecyclerOptions<POJO> options =
new FirebaseRecyclerOptions.Builder<POJO>()
.setQuery(query, POJO.class)
.build();
接下来创建FirebaseRecyclerAdapter对象:
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
// ...
}
};
此处有更多详细信息:
https://github.com/firebase/FirebaseUI-Android/tree/master/database