Home > Net >  I am trying to retrieved data from firestore in recyclerview in android studio but getting blank pag
I am trying to retrieved data from firestore in recyclerview in android studio but getting blank pag

Time:02-10

sorry in adavaced bcz i am first time askking question on stack flow so and if need any other please let me know okk my firestore Collestion is like these collection("user").document(currentUserId).collection("AddBlog").document and i need AddBlog document and last my field name of firestore is same to model.java variable

code of activity name 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();
            }
        });
    }
}

code of adpater class name 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);

        }
    }
}

code of my cardView xml file

<?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>

code of 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;
    }


}

CodePudding user response:

I think There are two mistake

1.Your adapter does not have the valid value when you set it for the recyclerview

2.Your data is null because you don't add any member to it as well as the adapter.

 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);

I guess this should be work.

  •  Tags:  
  • Related