如何在java(FireStore)中将每个数组项显示到recyclerview

问题描述 投票:0回答:1

我有一个名为 Treino 的集合,其中有包含数组练习的文档,我需要在回收器视图中显示该数组。 来自我的 Firebase 的图片

我正在使用带有 Java 的 android studio

我想在单独的 recyclerview 项目中显示每个数组项目,我如何使用 firebase firestore 在 java 中做到这一点

arrays firebase android-studio google-cloud-firestore android-recyclerview
1个回答
0
投票

第 1 步:设置 Firestore 和 RecyclerView 确保您的 build.gradle 中有以下依赖项:

implementation 'com.google.firebase:firebase-firestore:24.0.0' // Or latest version
implementation 'androidx.recyclerview:recyclerview:1.2.1' // Or latest version

第2步:创建模型类 为数组中的项目创建一个模型类。例如:

public class Exercicio {
    private String nome; // or any other fields you have

    public Exercicio() {
        // Default constructor required for calls to DataSnapshot.getValue(Exercicio.class)
    }

    public Exercicio(String nome) {
        this.nome = nome;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }
}

第3步:为RecyclerView创建适配器 为您的 RecyclerView 创建一个适配器类:

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class ExercicioAdapter extends RecyclerView.Adapter<ExercicioAdapter.ExercicioViewHolder> {
    private List<Exercicio> exercicios;

    public ExercicioAdapter(List<Exercicio> exercicios) {
        this.exercicios = exercicios;
    }

    @NonNull
    @Override
    public ExercicioViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
        return new ExercicioViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ExercicioViewHolder holder, int position) {
        Exercicio exercicio = exercicios.get(position);
        holder.textView.setText(exercicio.getNome());
    }

    @Override
    public int getItemCount() {
        return exercicios.size();
    }

    public static class ExercicioViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public ExercicioViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(android.R.id.text1);
        }
    }
}

第 4 步:从 Firestore 获取数据并更新 RecyclerView 在您的 Activity 或 Fragment 中,从 Firestore 获取数据并设置 RecyclerView:

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.ListenerRegistration;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private ExercicioAdapter adapter;
    private List<Exercicio> exercicioList;
    private FirebaseFirestore db;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        
        exercicioList = new ArrayList<>();
        adapter = new ExercicioAdapter(exercicioList);
        recyclerView.setAdapter(adapter);

        db = FirebaseFirestore.getInstance();
        fetchDataFromFirestore();
    }

    private void fetchDataFromFirestore() {
        db.collection("Treino").document("YOUR_DOCUMENT_ID").get()
            .addOnCompleteListener(task -> {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document.exists()) {
                        List<String> exercicios = (List<String>) document.get("exercicios");
                        if (exercicios != null) {
                            for (String nome : exercicios) {
                                exercicioList.add(new Exercicio(nome));
                            }
                            adapter.notifyDataSetChanged();
                        }
                    }
                }
            });
    }
}

第五步:Activity 布局 确保您的活动布局 (activity_main.xml) 包含 RecyclerView:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

© www.soinside.com 2019 - 2024. All rights reserved.