如何解决此错误?
错误:类中的构造函数FirebaseRecyclerAdapter FirebaseRecyclerAdapter不能应用于给定类型; 必需:找到FirebaseRecyclerOptions: 类,整数,类,数据库参考 原因:实际参数和形式参数列表的长度不同,其中T,VH 是类型变量:T扩展在类中声明的Object FirebaseRecyclerAdapter VH扩展了在类中声明的ViewHolder FirebaseRecyclerAdapter
mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
mFirebaseAdapter = new FirebaseRecyclerAdapter<FriendlyMessage,
MessageViewHolder>(
FriendlyMessage.class,
R.layout.activity_my_message,
MessageViewHolder.class,
mFirebaseDatabaseReference.child(MESSAGES_CHILD)) {
@Override
protected void populateViewHolder(MessageViewHolder viewHolder,
FriendlyMessage friendlyMessage, int position) {
mProgressBar.setVisibility(ProgressBar.INVISIBLE);
viewHolder.messageTextView.setText(friendlyMessage.getText());
viewHolder.messengerTextView.setText(friendlyMessage.getName());
if (friendlyMessage.getPhotoUrl() == null) {
viewHolder.messengerImageView
.setImageDrawable(ContextCompat
.getDrawable(Loand.this,
R.drawable.ic_account_circle_black_36dp));
} else {
Glide.with(Loand.this)
.load(friendlyMessage.getPhotoUrl())
.into(viewHolder.messengerImageView);
}
}
};
mFirebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
int friendlyMessageCount = mFirebaseAdapter.getItemCount();
int lastVisiblePosition =
mLinearLayoutManager.findLastCompletelyVisibleItemPosition();
// If the recycler view is initially being loaded or the
// user is at the bottom of the list, scroll to the bottom
// of the list to show the newly added message.
if (lastVisiblePosition == -1 ||
(positionStart >= (friendlyMessageCount - 1) &&
lastVisiblePosition == (positionStart - 1))) {
mMessageRecyclerView.scrollToPosition(positionStart);
}
}
});
mMessageRecyclerView.setLayoutManager(mLinearLayoutManager);
mMessageRecyclerView.setAdapter(mFirebaseAdapter);
答案 0 :(得分:0)
您遇到以下错误:
错误:错误:类FirebaseRecyclerAdapter中的构造函数FirebaseRecyclerAdapter无法应用于给定类型;必需:找到的FirebaseRecyclerOptions:类,整数,类,数据库参考原因:实际和形式参数列表的长度不同,其中T,VH是类型变量:T扩展了在FirebaseRecyclerAdapter类中声明的对象VH扩展了在FirebaseRecyclerAdapter类中声明的ViewHolder
因为您已在build.gradle文件中添加了Firebase-UI库依赖项的最新版本。为了能够创建FirebaseRecyclerAdapter
类的新对象,需要一个FirebaseRecyclerOptions
对象。您正在做的是将错误的元素传递给构造函数。在较早的版本中可以使用这种创建适配器的方式。现在,要创建新对象,请传递FirebaseRecyclerOptions
对象,如以下帖子中我的回答所述: