Recycler View Android中的Firestore数据[重复]

Posted

技术标签:

【中文标题】Recycler View Android中的Firestore数据[重复]【英文标题】:Firestore Data in Recycler View Android [duplicate] 【发布时间】:2022-01-03 12:47:05 【问题描述】:

我正在将我的 Firestore 数据检索到 java android 中的回收站视图。我的数据看起来首先是一个名称“用户”的集合,而不是一些具有唯一 ID 的文档,并且所有文档都包含名称、等级和图像,但是在检索这些数据时,我无法在我的回收站视图中看到任何内容

模型类


public class UserModel 
    String name, grade ,image;

    public UserModel() 
    

    public String getName() 
        return name;
    

    public void setName(String name) 
        this.name = name;
    

    public String getGrade() 
        return grade;
    

    public void setGrade(String grade) 
        this.grade = grade;
    

    public String getImage() 
        return image;
    

    public void setImage(String image) 
        this.image = image;
    

    public UserModel(String name, String grade, String image) 
        this.name = name;
        this.grade = grade;
        this.image = image;
    

适配器类


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.programiner.studyvillaadmin.R;

public class UserAdapter extends FirestoreRecyclerAdapter<UserModel, UserAdapter.ViewHolder> 
    public UserAdapter(@NonNull FirestoreRecyclerOptions<UserModel> options) 
        super(options);
    

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull UserModel model) 
        holder.username.setText(model.getName());
        holder.usergrade.setText(model.getGrade());
        Glide.with(holder.itemView.getContext()).load(model.getImage()).into(holder.profile_img);
    

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_row, parent, false);
        return new ViewHolder(view);
    


    public class ViewHolder extends RecyclerView.ViewHolder 
        ImageView profile_img;
        TextView username, usergrade;

        public ViewHolder(@NonNull View itemView) 
            super(itemView);

            profile_img = itemView.findViewById(R.id.imageView2);
            username = itemView.findViewById(R.id.textView12);
            usergrade = itemView.findViewById(R.id.textView13);

        
    

活动代码


public class User extends AppCompatActivity 

    ActivityUserBinding binding;
    UserAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        binding = ActivityUserBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initrecyclerview();


    

    private void initrecyclerview() 


        Query query = FirebaseFirestore.getInstance()
                .collection("Users");

        FirestoreRecyclerOptions<UserModel> options = new FirestoreRecyclerOptions.Builder<UserModel>()
                .setQuery(query, UserModel.class)
                .build();


        binding.rvUser.setLayoutManager(new LinearLayoutManager(this));
        adapter = new UserAdapter(options);
        binding.rvUser.setAdapter(adapter);

    

LogCat 错误

2021-11-25 13:44:09.157 3751-3751/? E/YouTube: flushBinderConnectionCallbacks is unverified on SDK 28
2021-11-25 13:44:09.885 1883-1896/? E/memtrack: Couldn't load memtrack module
2021-11-25 13:44:10.610 2310-2310/? E/BluetoothAdapter: Bluetooth binder is null
2021-11-25 13:44:10.857 3911-3911/? E/oadcastreceive: Not starting debugger since process cannot load the jdwp agent.
2021-11-25 13:44:10.909 1840-1840/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2021-11-25 13:44:10.910 1840-1840/? E/netmgr: WifiForwarder unable to open QEMU pipe: Invalid argument
2021-11-25 13:44:11.462 3751-3814/? E/FirebaseInstanceId: Token retrieval failed: INVALID_SENDER
2021-11-25 13:44:11.526 2310-3941/? E/bjyh: RuntimeException while executing runnable bjyujhb@b76ad74 with executor MoreExecutors.directExecutor()
    java.lang.NullPointerException: Attempt to get length of null array
        at android.util.Base64.encode(Base64.java:494)
        at android.util.Base64.encodeToString(Base64.java:456)
        at com.google.android.gms.auth.proximity.RemoteDevice.a(:com.google.android.gms@214218031@21.42.18 (100700-410302452):0)
        at jhb.b(:com.google.android.gms@214218031@21.42.18 (100700-410302452):2)
        at bjyu.run(:com.google.android.gms@214218031@21.42.18 (100700-410302452):6)
        at bjye.execute(:com.google.android.gms@214218031@21.42.18 (100700-410302452):0)
        at bjyh.c(:com.google.android.gms@214218031@21.42.18 (100700-410302452):1)
        at bjyh.b(:com.google.android.gms@214218031@21.42.18 (100700-410302452):3)
        at rnh.done(:com.google.android.gms@214218031@21.42.18 (100700-410302452):0)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
        at java.util.concurrent.FutureTask.set(FutureTask.java:234)
        at java.util.concurrent.FutureTask.run(FutureTask.java:274)
        at rne.c(:com.google.android.gms@214218031@21.42.18 (100700-410302452):6)
        at rne.run(:com.google.android.gms@214218031@21.42.18 (100700-410302452):7)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at rrk.run(:com.google.android.gms@214218031@21.42.18 (100700-410302452):0)
        at java.lang.Thread.run(Thread.java:764)
2021-11-25 13:44:11.631 3946-3946/? E/droid.apps.map: Not starting debugger since process cannot load the jdwp agent.
2021-11-25 13:44:12.316 3946-3975/? E/droid.apps.map: The String#value field is not present on Android versions >= 6.0
2021-11-25 13:44:12.506 2456-3449/? E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!  (parcel size = 1056768)
2021-11-25 13:44:12.756 4001-4001/? E/d.process.medi: Not starting debugger since process cannot load the jdwp agent.
2021-11-25 13:44:12.848 4016-4016/? E/id.gms.unstabl: Not starting debugger since process cannot load the jdwp agent.
2021-11-25 13:44:13.210 2456-3878/? E/gle.android.gm: Invalid ID 0x00000000.
2021-11-25 13:44:13.308 2456-3878/? E/gle.android.gm: Invalid ID 0x00000000.
2021-11-25 13:44:13.535 1883-1896/? E/memtrack: Couldn't load memtrack module
2021-11-25 13:44:13.760 2456-3953/? E/WakeLock: DG:service ** IS FORCE-RELEASED ON TIMEOUT **
2021-11-25 13:44:14.436 2456-3874/? E/PhenotypeFlagCommitter: Retrieving snapshot for com.google.android.gms.playlog.uploader failed
    java.util.concurrent.TimeoutException: Timed out waiting for Task
        at aqlt.I(:com.google.android.gms@214218031@21.42.18 (100700-410302452):6)
        at akob.c(:com.google.android.gms@214218031@21.42.18 (100700-410302452):2)
        at akob.i(:com.google.android.gms@214218031@21.42.18 (100700-410302452):2)
        at akob.f(:com.google.android.gms@214218031@21.42.18 (100700-410302452):0)
        at com.google.android.gms.clearcut.init.ClearcutInitChimeraIntentService.e(:com.google.android.gms@214218031@21.42.18 (100700-410302452):5)
        at pqt.onHandleIntent(:com.google.android.gms@214218031@21.42.18 (100700-410302452):4)
        at com.google.android.chimera.IntentOperation.onHandleIntent(:com.google.android.gms@214218031@21.42.18 (100700-410302452):2)
        at pqo.onHandleIntent(:com.google.android.gms@214218031@21.42.18 (100700-410302452):4)
        at drp.run(:com.google.android.gms@214218031@21.42.18 (100700-410302452):5)
        at dro.run(:com.google.android.gms@214218031@21.42.18 (100700-410302452):11)
        at bfzx.run(:com.google.android.gms@214218031@21.42.18 (100700-410302452):2)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
2021-11-25 13:44:14.724 4096-4096/? E/d.process.acor: Not starting debugger since process cannot load the jdwp agent.
2021-11-25 13:44:16.327 2310-4135/? E/BluetoothAdapter: Bluetooth binder is null
2021-11-25 13:44:16.778 1751-1794/? E/storaged: getDiskStats failed with result NOT_SUPPORTED and size 0
2021-11-25 13:44:18.560 1883-1896/? E/memtrack: Couldn't load memtrack module
2021-11-25 13:44:18.826 2456-4134/? E/PhenotypeFlagCommitter: Retrieving snapshot for com.google.lighter.android#com.google.android.gms failed
    java.util.concurrent.TimeoutException: Timed out waiting for Task
        at aqlt.I(:com.google.android.gms@214218031@21.42.18 (100700-410302452):6)
        at akob.c(:com.google.android.gms@214218031@21.42.18 (100700-410302452):2)
        at akob.i(:com.google.android.gms@214218031@21.42.18 (100700-410302452):2)
        at akob.f(:com.google.android.gms@214218031@21.42.18 (100700-410302452):0)
        at awfk.apply(:com.google.android.gms@214218031@21.42.18 (100700-410302452):11)
        at bjww.b(:com.google.android.gms@214218031@21.42.18 (100700-410302452):2)
        at bjwx.run(:com.google.android.gms@214218031@21.42.18 (100700-410302452):9)
        at bjye.execute(:com.google.android.gms@214218031@21.42.18 (100700-410302452):0)
        at bjwt.q(:com.google.android.gms@214218031@21.42.18 (100700-410302452):1)
        at bjwt.j(:com.google.android.gms@214218031@21.42.18 (100700-410302452):8)
        at bjwt.m(:com.google.android.gms@214218031@21.42.18 (100700-410302452):0)
        at bkag.e(:com.google.android.gms@214218031@21.42.18 (100700-410302452):0)
        at bjzh.run(:com.google.android.gms@214218031@21.42.18 (100700-410302452):6)
        at bkah.run(:com.google.android.gms@214218031@21.42.18 (100700-410302452):0)
        at awfo.run(:com.google.android.gms@214218031@21.42.18 (100700-410302452):2)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at xuz.run(:com.google.android.gms@214218031@21.42.18 (100700-410302452):14)
        at java.lang.Thread.run(Thread.java:764)
2021-11-25 13:44:19.711 1883-1896/? E/memtrack: Couldn't load memtrack module
2021-11-25 13:44:19.896 2999-2999/? E/Finsky: [2] CheckinReceiver.c(2): Receiver disabled.
2021-11-25 13:44:21.376 1883-1896/? E/memtrack: Couldn't load memtrack module
2021-11-25 13:44:21.972 1883-1896/? E/memtrack: Couldn't load memtrack module
2021-11-25 13:44:21.994 1883-1896/? E/memtrack: Couldn't load memtrack module
2021-11-25 13:44:22.800 2310-4135/? E/BluetoothAdapter: Bluetooth binder is null
2021-11-25 13:44:23.089 3218-3230/? E/CarrierServices: [146] iez.getFile: File not found.: /data/user/0/com.google.android.apps.messaging/files/httpft_pending (No such file or directory)
        java.io.FileInputStream.open0(Native Method)
        java.io.FileInputStream.open(FileInputStream.java:231)
        java.io.FileInputStream.<init>(FileInputStream.java:165)
        android.app.ContextImpl.openFileInput(ContextImpl.java:560)
        android.content.ContextWrapper.openFileInput(ContextWrapper.java:202)
        iez.getFile(SourceFile:20)
        com.google.android.apps.messaging.rcsmigration.RcsStateProvider.buildRcsState(SourceFile:136)
        com.google.android.apps.messaging.rcsmigration.RcsStateProvider.getRcsState(SourceFile:6)
        com.google.android.ims.rcsmigration.IRcsStateProvider$Stub.dispatchTransaction(SourceFile:10)
        com.google.android.aidl.BaseStub.onTransact(SourceFile:18)
        android.os.Binder.execTransact(Binder.java:731)

解决方案

@Override
protected void onStart() 
    super.onStart();
    adapter.startListening();


@Override
protected void onStop() 
    super.onStop();
    adapter.stopListening();

【问题讨论】:

您在哪一行代码中遇到了 NullPointerException? @AlexMamo 在 logcat 中,没有任何特定的行来自我从哪里得到 nullpointerexception... 这个错误很可能,java.lang.NullPointerException: Attempt to get length of null array 与 Firebase 无关。 受保护的 void onStart() super.onStart();适配器.startListening(); @Override 受保护的无效 onStop() super.onStop();适配器.stopListening(); 很高兴听到您的问题得到解决。如果您可以将其作为答案发布,将对社区成员有所帮助。 【参考方案1】:

解决方案

确保您在适配器上调用 start listener 以启动与 Cloud Firestore 的连接,否则将不会加载任何数据。

@Override
    protected void onStart() 
        super.onStart();
        adapter.startListening();
    
    
    @Override
    protected void onStop() 
        super.onStop();
        adapter.stopListening();
    

【讨论】:

以上是关于Recycler View Android中的Firestore数据[重复]的主要内容,如果未能解决你的问题,请参考以下文章

Android recycler view onItemClick 识别适配器

我想使用 GeoFire 在 android 中填充 Firebase Recycler View

谁能建议如何在android Recycler View中按产品价格实现排序[关闭]

在 Android 的 Recycler View 中生成和设置文本视图背景的随机颜色

Recycler View重复,如何查看一次

在 Recycler View 中,从 LinearLayoutManger 转换为 GridLayoutManager 没有区别