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

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 :

Logs with Tag : Datacheck
Log with Tag : IMOP

Kindly please help with this,
thanks in advance.

Bhushan Patil
  • 71
  • 1
  • 3
  • The problem, in your code, is the name of the fields in the class and the ones in the database. Please check the duplicate answers to see how you can solve this. – Alex Mamo Jul 27 '21 at 15:02
  • @AlexMamo I've been trying for two days for this, and i even tried by putting similiar names but in that case not even single record is being displayed please don't close the question, the duplicate answers are for Firebase realtime db, i am using firestore – Bhushan Patil Jul 27 '21 at 15:06
  • 1
    The solution is the same between the two databases. Since the property names in your database start with an uppercase characters, you'll need to add `@PropertyName("ItemName")` annotations on your code to get the correct mapping. Also see https://stackoverflow.com/a/58026286 and https://stackoverflow.com/a/50715721. – Frank van Puffelen Jul 27 '21 at 15:21
  • The POJO classes are the same for both databases. Please use annotations, as explained in the duplicate answer and your problem will be solved. – Alex Mamo Jul 27 '21 at 15:21
  • i've updated the code and change the variable name to the exact name of firestore property. but now the app isn't showing a single record, ive also updated the image of firestore, please help – Bhushan Patil Jul 27 '21 at 15:35
  • @AlexMamo please check – Bhushan Patil Jul 27 '21 at 15:55

0 Answers0