SetAdapter 不能作为 FirestoreRecyclerviewAdapter 的适配器应用
Posted
技术标签:
【中文标题】SetAdapter 不能作为 FirestoreRecyclerviewAdapter 的适配器应用【英文标题】:SetAdapter cannot be applied as the adapter of FirestoreRecyclerviewAdapter 【发布时间】:2021-11-04 20:55:48 【问题描述】:我正在使用 firebase 开发一个聊天应用程序,我想将用户信息匹配到 recyclerview...当我将适配器设置为 recyclerview 时,我的代码是正确的,它显示错误。 这是我的代码:
package com.abdelatif.chatapp;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.squareup.picasso.Picasso;
public class ChatFragment extends Fragment
private FirebaseFirestore firebaseFirestore;
LinearLayoutManager linearLayoutManager;
private FirebaseAuth firebaseAuth;
ImageView mimageviewofuser;
FirestoreRecyclerAdapter<FireBaseModel, NoteViewHolder> chatAdapter = null;
RecyclerView mrecyclerview;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View v=inflater.inflate(R.layout.chatfragment,container,false);
firebaseAuth=FirebaseAuth.getInstance();
firebaseFirestore= FirebaseFirestore.getInstance();
mrecyclerview=v.findViewById(R.id.recyclerView);
// Query query=firebaseFirestore.collection("Users");
Query query=firebaseFirestore.collection("Users").whereNotEqualTo("uid",firebaseAuth.getUid());
FirestoreRecyclerOptions<FireBaseModel> allusername=new FirestoreRecyclerOptions.Builder<FireBaseModel>().setQuery(query,FireBaseModel.class).build();
chatAdapter=new FirestoreRecyclerAdapter<FireBaseModel, NoteViewHolder>(allusername)
@Override
protected void onBindViewHolder(@NonNull NoteViewHolder noteViewHolder, int i, @NonNull FireBaseModel firebasemodel)
noteViewHolder.particularusername.setText(firebasemodel.getName());
String uri=firebasemodel.getImage();
Picasso.get().load(uri).into(mimageviewofuser);
if(firebasemodel.getStatus().equals("Online"))
noteViewHolder.statusofuser.setText(firebasemodel.getStatus());
noteViewHolder.statusofuser.setTextColor(Color.GREEN);
else
noteViewHolder.statusofuser.setText(firebasemodel.getStatus());
noteViewHolder.itemView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Toast.makeText(getActivity(), "contactClicked", Toast.LENGTH_SHORT).show();
);
@NonNull
@Override
public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_view_layout,parent,false);
return new NoteViewHolder(view);
;
mrecyclerview.setHasFixedSize(true);
linearLayoutManager=new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
mrecyclerview.setLayoutManager(linearLayoutManager);
mrecyclerview.setAdapter(chatAdapter);
chatAdapter.startListening();
return v;
public class NoteViewHolder extends android.support.v7.widget.RecyclerView.ViewHolder
private TextView particularusername;
private TextView statusofuser;
public NoteViewHolder(@NonNull View itemView)
super(itemView);
particularusername=itemView.findViewById(R.id.nameOfUser);
statusofuser=itemView.findViewById(R.id.statusOfUser);
mimageviewofuser=itemView.findViewById(R.id.imageViewOfUser);
错误在一行:mrecyclerview.setAdapter(chatAdapter);
错误是:
'androidx.recyclerview.widget.RecyclerView'中的'setAdapter(androidx.recyclerview.widget.RecyclerView.Adapter)'不能应用于'(com.firebase.ui.firestore.FirestoreRecyclerAdapter
我希望有人给我一个解决方案...我搜索了很多次但没有。
【问题讨论】:
能否请您检查following article helps you 是否为Android 和Firebase 提供的以下文档? 【参考方案1】:确保在拨打mrecyclerview.setAdapter(chatAdapter);
之前先拨打chatAdapter.startListening();
【讨论】:
是的,兄弟,它解决了我的问题..我的错是我在设置适配器后调用它...我明白....非常感谢查理回答我以上是关于SetAdapter 不能作为 FirestoreRecyclerviewAdapter 的适配器应用的主要内容,如果未能解决你的问题,请参考以下文章
setAdapter“调用需要 API 级别 11(当前最低为 8):android.widget.AbsListView#setAdapter”?
Firebase firestore 云函数显示错误:无效使用类型“未定义”作为 Firestore 参数
将 Firestore 文档作为纯 Javascript 对象获取?
使用 firebase_admin 或 google.cloud.firestore 在 python 中的 Firestore 客户端(作为用户)