繁体   English   中英

我正在尝试从 android studio 的 recyclerview 中的 firestore 中检索数据,但得到空白页面并且没有错误我尝试了很多方法但没有工作

[英]I am trying to retrieved data from firestore in recyclerview in android studio but getting blank page and no error i have tried many way but not work

抱歉,在 adavaced bcz 我是第一次问关于堆栈流的问题所以如果需要任何其他请让我知道我的 firestore Collestion 就像这些 collection("user").document(currentUserId).collection("AddBlog").document我需要 AddBlog 文档,最后我的 firestore 字段名称与 model.java 变量相同

活动名称代码 Trial.java

package com.example.talkvirtual;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.ArrayList;
import java.util.List;

public class TrialActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    ArrayList<Model> dataList;

    FirebaseFirestore db;
    FirebaseAuth mAuth;

    MyAdapter myAdapter;

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

        recyclerView = findViewById(R.id.recyclerViewTrial);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        dataList = new ArrayList<>();
        recyclerView.setAdapter(myAdapter);

        myAdapter = new MyAdapter(dataList,this);
        db = FirebaseFirestore.getInstance();
        mAuth= FirebaseAuth.getInstance();
        String currentUserId = mAuth.getCurrentUser().getUid();
        db.collection("user").document(currentUserId).collection("AddBlog").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                List<DocumentSnapshot> list = queryDocumentSnapshots.getDocuments();
                for (DocumentSnapshot d : list) {
                    Model model = d.toObject(Model.class);
                    dataList.add(model);
                }
                myAdapter.notifyDataSetChanged();
            }
        });
    }
}

适配器代码 class 名称 MyAdapter.java

package com.example.talkvirtual;

import android.content.Context;
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.ArrayList;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {


    public MyAdapter(ArrayList<Model> listblogProfile, Context context) {
        this.ListblogProfile = listblogProfile;
        this.context = context;
    }

    private ArrayList<Model> ListblogProfile;
  private Context context;


  //  private ShowActivity activity;

    @NonNull
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.container_blog, parent, false);
        return new MyViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {
        Model model = ListblogProfile.get(position);

        holder.blogTitleTv.setText(model.getBlogTitle());
        holder.blogContentTv.setText(model.getBlogContent());
    }

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


    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView blogTitleTv, blogContentTv;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            blogContentTv = itemView.findViewById(R.id.blogContentContainer);
            blogTitleTv = itemView.findViewById(R.id.getTitleContainer);

        }
    }
}

我的卡的代码查看 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/layoutNote"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:foreground="?attr/selectableItemBackgroundBorderless"
    app:cardElevation="8dp"
    android:layout_margin="10dp"
    android:background="@color/light_dark">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/getTitleContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Title"
            android:textColor="@color/textColor"
            android:textSize="24dp"
            android:textStyle="bold"
            android:padding="10dp"
            />

        <TextView
            android:id="@+id/blogContentContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxHeight="100dp"
            android:textColor="@color/textColor"
            android:hint="Blog Content"
            android:textColorHint="@color/gray"
            android:textSize="20dp"
            android:padding="10dp"/>
    </LinearLayout>

</androidx.cardview.widget.CardView>

代码 model class

  package com.example.talkvirtual;

public class Model {
    String blogTitle;
    String blogContent;
    String id;
    public Model(){}//empty constructor is neccessary firebase

    public Model(String blogTitle, String blogContent, String id) {
        this.blogTitle = blogTitle;
        this.blogContent = blogContent;
        this.id = id;
    }
    public String getBlogTitle() {
        return blogTitle;
    }

    public void setBlogTitle(String blogTitle) {
        this.blogTitle = blogTitle;
    }

    public String getBlogContent() {
        return blogContent;
    }

    public void setBlogContent(String blogContent) {
        this.blogContent = blogContent;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }


}

我认为有两个错误

1.当你为recyclerview设置适配器时,你的适配器没有有效值

2.您的数据是 null 因为您没有向它添加任何成员以及适配器。

 db.collection("user").document(currentUserId).collection("AddBlog").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
        @Override
        public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
            List<DocumentSnapshot> list =     queryDocumentSnapshots.getDocuments();
            for (DocumentSnapshot d : list) {
                Model model = d.toObject(Model.class);
                dataList.add(model);
            }
            myAdapter.notifyDataSetChanged();
        }
    });
  myAdapter = new MyAdapter(dataList,this);
  recyclerView.setAdapter(myAdapter);

我想这应该是工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM