0

I am trying to fetch a students data from Firestore Collection and then display it in Recycler view My Firestore Collection and Document : Collection Name is School Code, Document contains Student Info

Model Class :(Student_Model_Class)

package com.example.firestooredemo;

import java.sql.Timestamp;
import java.util.HashMap;

public class sdm {
    private HashMap<String, String> QnA;
    private String formname;
    private String schoolid;
    private String standard;
    private String studentname;
    private String subject;
    private Timestamp timestamp;

    private sdm(){}

    private sdm(HashMap qna,String formname,String schoolid,String standard,String studentname,String subject,Timestamp timestamp){
        this.QnA=qna;
        this.formname=formname;
        this.schoolid=schoolid;
        this.standard=standard;
        this.studentname=studentname;
        this.subject=subject;
        this.timestamp=timestamp;
    }

    public HashMap<String, String> getQnA() {
        return QnA;
    }

    public void setQnA(HashMap<String, String> qnA) {
        QnA = qnA;
    }

    public String getFormname() {
        return formname;
    }

    public void setFormname(String formname) {
        this.formname = formname;
    }

    public String getSchoolid() {
        return schoolid;
    }

    public void setSchoolid(String schoolid) {
        this.schoolid = schoolid;
    }

    public String getStandard() {
        return standard;
    }

    public void setStandard(String standard) {
        this.standard = standard;
    }

    public String getStudentname() {
        return studentname;
    }

    public void setStudentname(String studentname) {
        this.studentname = studentname;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getTimestamp() {
        return timestamp.toString();
    }

    public void setTimestamp(Timestamp timestamp) {
        this.timestamp = timestamp;
    }
}

Activity.java :

package com.example.firestooredemo;

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

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.List;

public class DisplayRecord extends AppCompatActivity {
    TextView Record_Title,DemoD;
    private FirebaseFirestore Fs;
    private RecyclerView FSList;
    private FirestoreRecyclerAdapter<sdm,Student_View_Holder> F_Adapter;
    String S_ID,Student;
    List<sdm> stud;
    int S_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_record);
        FSList=(RecyclerView) findViewById(R.id.RCView);
        FSList.setHasFixedSize(true);
        FSList.setLayoutManager(new LinearLayoutManager(this));
        Intent intent = getIntent();
        Fs=FirebaseFirestore.getInstance();
        Record_Title= (TextView) findViewById(R.id.RecordTitle);
        S_ID = intent.getStringExtra("School ID");
        Record_Title.setText("Records of "+S_ID);
        Log.d("Datacheck","School :"+S_ID);
        S_id=Integer.parseInt(S_ID);
        String TAG="Datacheck";

        Fs.collection("EM_DEMO2").document("10th_STD").collection(S_ID)
                .whereEqualTo("formname","Tourism, Transport and Communication3")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                Log.d(TAG, document.getId() + " => " + document.getData().toString());
                                Student=Student+document.getLong("standard")+"\n"+document.get("subject")+"\n"+document.get("formname")+"\n-----\n";
//                                stud.add(document.toObject(s1));
                                Log.d("IMOP",Student);
                            }
                        } else {
                            Log.d(TAG, "Error getting documents: ", task.getException());
                        }
                    }
                });
        //Query Part
        Query Q=Fs.collection("EM_DEMO2").document("10th_STD").collection(S_ID).whereEqualTo("formname","Tourism, Transport and Communication3");

        // Recycler view Display
        FirestoreRecyclerOptions<sdm> Opt=new FirestoreRecyclerOptions.Builder<sdm>()
                .setQuery(Q,sdm.class)
                .build();

        F_Adapter = new FirestoreRecyclerAdapter<sdm, Student_View_Holder>(Opt) {
            @NonNull
            @Override
            public Student_View_Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View V= LayoutInflater.from(parent.getContext()).inflate(R.layout.student_data,parent,false);
                return new Student_View_Holder(V);
            }

            @Override
            protected void onBindViewHolder(@NonNull Student_View_Holder holder, int position, @NonNull sdm model) {
                holder.StudentName.setText(model.getStudentname());
                holder.Standard.setText(model.getStandard());
                holder.SchoolID.setText(model.getSchoolid()+"");
                holder.Subject.setText(model.getSubject());
                holder.FormName.setText(model.getFormname());
//                holder.Time.setText(model.getTs()+"");
            }
        };


        FSList.setAdapter(F_Adapter);

        //View Holder
    }

    private class Student_View_Holder extends RecyclerView.ViewHolder {
        private TextView StudentName,Standard,Subject,SchoolID,FormName,Time;
        public Student_View_Holder(@NonNull View itemView) {
            super(itemView);
            StudentName = itemView.findViewById(R.id.StudentName);
            Standard = itemView.findViewById(R.id.Std);
            Subject = itemView.findViewById(R.id.Sub);
            SchoolID = itemView.findViewById(R.id.SchoolID);
            FormName = itemView.findViewById(R.id.FormName);
            Time = itemView.findViewById(R.id.Timestamp);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        F_Adapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(F_Adapter!= null){
            F_Adapter.stopListening();
        }
    }
}

Student_data design : XML Design Screenshot But In app Recycler view only display fetched values of Subject,

App output isn't showing any showing any record but just and empty recycler view.

I put some log messages in Student Data Model Class,to check if data is being fetched or not. But those logs doesn't even show up Log Messages with tag ValueCheck and Datacheck aren't showing up.

Kindly please help with this,
thanks in advance.

Bhushan Patil
  • 71
  • 1
  • 3
  • If you try to log the value of `model.getStudentname()` inside onBindViewHolder, do you get something printed out? – Alex Mamo Jul 28 '21 at 06:28
  • @AlexMamo Nothing is getting printed. – Bhushan Patil Jul 28 '21 at 08:33
  • Is your onBindViewHolder even triggered? – Alex Mamo Jul 28 '21 at 10:00
  • @AlexMamo I don't think so, but afterwards i commented the code below Query Part and tried to run it and even in that " Fs.collection("EM_DEMO2").document("10th_STD").collection(S_ID) " this code and the uncommented part didn't even get triggered, i don't know why. – Bhushan Patil Jul 28 '21 at 14:07
  • @AlexMamo After a putting some logs in some part of code, I realised the the " Task task " is returning empty List, here's screenshot https://imgur.com/DDD8Epu – Bhushan Patil Jul 28 '21 at 14:33
  • According to your last comment, I can say that this is a classic issue regarding asynchronous programming. Any code that needs data from the database, needs to be inside the onComplete/onSuccess method, or be called from there. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a custom callback. – Alex Mamo Jul 28 '21 at 14:49
  • @AlexMamo The query code is inside of the Oncomplete method and the problem is getting weird i have two main collections which holds exact same structure of data but only different collection name, EM_DEMO and EM_DEMO2. and if i change the query collection from EM_DEMO2 to EM_DEMO logs are displaying but not every data is being displayed, Sir can you please help me in this, I have discord on my pc so we can communicate via it, thank you – Bhushan Patil Jul 28 '21 at 15:11
  • Please try to take into consideration my advice from my last comment. – Alex Mamo Jul 28 '21 at 15:18

0 Answers0