Devika Sujith
Devika Sujith

Reputation: 121

My RecyclerView is not showing any data - Firebase

I'm following a tutorial on youtube and trying to display firebase data as recycler view. However, it is completely blank in my layout. I cannot understand why. I've gone through similar questions here and tried all the solutions but none work. Please help me figure this out.Thanks.

Following is my firebase db.

enter image description here

Home2.java

package com.example.oddsynew;

import android.os.Bundle;
import android.widget.Toast;

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

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class Home2 extends AppCompatActivity {

    DatabaseReference myRef;
    RecyclerView newJobList;
    ArrayList<Job> list;
    HomeAdapter adapter;

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

        newJobList = (RecyclerView) findViewById(R.id.newJobs);
        newJobList.setLayoutManager(new LinearLayoutManager(this));
        list = new ArrayList<Job>();

        adapter = new HomeAdapter(Home2.this, list);
        newJobList.setAdapter(adapter);

        myRef = FirebaseDatabase.getInstance().getReference().child("Job");
        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot ds: dataSnapshot.getChildren()){
                    Job j = ds.getValue(Job.class);
                    list.add(j);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(Home2.this, "Error", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

This is my layout.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Home"
    >
    <SearchView
        android:id="@+id/search"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="80dp"
        android:background="#fff"
        android:iconifiedByDefault="false"
        android:queryBackground="@android:color/transparent"
        android:queryHint="Search Jobs"
        android:textColor="#fff"
        android:textSize="10dp"
        android:visibility="visible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/newBtn"
        style="@style/TextButton"
        android:layout_width="30dp"
        android:layout_height="27dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="24dp"
        android:text="New"
        android:textAlignment="viewStart"
        android:textSize="14sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/search" />

    <Button
        android:id="@+id/nearYouBtn"
        style="@style/TextButton"
        android:layout_width="70dp"
        android:layout_height="30dp"
        android:layout_marginStart="70dp"
        android:layout_marginTop="24dp"
        android:text="Near You"
        android:textAlignment="viewStart"
        android:textSize="14sp"
        app:layout_constraintStart_toEndOf="@+id/newBtn"
        app:layout_constraintTop_toBottomOf="@+id/search" />

    <Button
        android:id="@+id/recommendBtn"
        style="@style/TextButton"
        android:layout_width="96dp"
        android:layout_height="25dp"
        android:layout_marginStart="70dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="16dp"
        android:text="Recommended"
        android:textAlignment="viewStart"
        android:textSize="14sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/nearYouBtn"
        app:layout_constraintTop_toBottomOf="@+id/search" />

This is the model class.

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/newJobs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="28dp"
            android:layout_marginEnd="16dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/nearYouBtn" />


    </androidx.constraintlayout.widget.ConstraintLayout>

package com.example.oddsynew;

public class Job {
    private String job_name;
    private String recruiter_name;
    private String location;
    private String job_charge;

    public Job() {
    }

    public Job(String job_name, String recruiter_name, String location, String job_charge) {
        this.job_name = job_name;
        this.recruiter_name = recruiter_name;
        this.location = location;
        this.job_charge = job_charge;
    }

    public String getJob_name() {
        return job_name;
    }

    public void setJob_name(String job_name) {
        this.job_name = job_name;
    }

    public String getRecruiter_name() {
        return recruiter_name;
    }

    public void setRecruiter_name(String recruiter_name) {
        this.recruiter_name = recruiter_name;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getJob_charge() {
        return job_charge;
    }

    public void setJob_charge(String job_charge) {
        this.job_charge = job_charge;
    }
}

And my Home Adapter class.

package com.example.oddsynew;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

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

    Context context;
    ArrayList<Job> jobs;

    public HomeAdapter(Context c, ArrayList<Job> j){
        context = c;
        jobs = j;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.job_row, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.recruiterName.setText(jobs.get(position).getRecruiter_name());
        holder.jobName.setText(jobs.get(position).getJob_name());
        holder.jobLocation.setText(jobs.get(position).getLocation());
        holder.jobCharge.setText(jobs.get(position).getJob_charge());
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView recruiterName, jobName, jobLocation, jobCharge;
        ImageView profPic;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            recruiterName = itemView.findViewById(R.id.recruiterName);
            jobName = itemView.findViewById(R.id.jobName);
            jobLocation = itemView.findViewById(R.id.jobLocation);
            jobCharge = itemView.findViewById(R.id.jobCharge);
            profPic = itemView.findViewById(R.id.prof_pic);
        }
    }
}

Upvotes: 1

Views: 75

Answers (2)

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

  1. You need to call adapter.notifyDataSetChanged() after the for loop to inform the adapter to reprocess the data after updates in the data source(list) as

    for (DataSnapshot ds: dataSnapshot.getChildren()){
        Job j = ds.getValue(Job.class);
        list.add(j);
    }
    adapter.notifyDataSetChanged();
    // ^^^^^^^^^^^^^^^^^^^^^^^
    
  2. To get the children, use Jobs instead of Job as

    myRef = FirebaseDatabase.getInstance().getReference().child("Jobs");
    //                                                           ^^^^
    

Upvotes: 1

Himshikhar Gayan
Himshikhar Gayan

Reputation: 26

Replace

 myRef = FirebaseDatabase.getInstance().getReference().child("Job");

with

 myRef = FirebaseDatabase.getInstance().getReference().child("Jobs");

and add adapter.notifyDataSetChanged() as follows

@Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot ds: dataSnapshot.getChildren()){
                Job j = ds.getValue(Job.class);
                list.add(j);
            }
            adapter.notifyDataSetChanged();
        }

Upvotes: 1

Related Questions