Data from firebase not showing up onclick recyclerview

Viewed 350

I have a recycler list view and when one is clicked I'd like to open a new page with its info. When I try to display the job name, location and job charge in the new page, it shows up. However, when I'm trying to display job_date. My app crashes. I don't know why since I'm following the same method to display data. When I comment out wherever job_date occurs, it works normally.

EDIT: I'd like to clarify by crash I mean it doesn't close immediately. It works till I get to the listview page and then it just suddenly goes back to my main activity page which just has a button to proceed to the next page.

I only get these two errors in my logcat but I'm not sure if it has anything to do with the problem:

03-14 14:11:06.634 3417-3423/? E/art: Failed sending reply to debugger: Broken pipe

03-14 14:11:07.165 3417-3417/? E/HAL: load: id=gralloc != hmi->id=gralloc

I have a Firebase database as follows:

enter image description here

This is my main Home class:

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 Home extends AppCompatActivity {

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

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

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

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



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

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

This is my job info class

package com.example.oddsynew;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.squareup.picasso.Picasso;

public class JobInfo extends AppCompatActivity {

    TextView jobName, jobCharge, jobLocation, jobDate;
    ImageView profPic;
    String jobname, jobloc, jobcharge, profpic, jobdate;


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

        jobName = (TextView) findViewById(R.id.jobName_info);
        jobCharge = (TextView) findViewById(R.id.jobCharge_info);
        jobLocation = (TextView) findViewById(R.id.jobLocation_info);
        profPic = (ImageView) findViewById(R.id.prof_pic_info);
        //jobDate = (TextView) findViewById(R.id.jobDate_info);

        jobname = getIntent().getStringExtra("jobName");
        jobloc = getIntent().getStringExtra("jobLocation");
        jobcharge = getIntent().getStringExtra("jobCharge");
        profpic = getIntent().getStringExtra("profPic");
        //jobdate = getIntent().getStringExtra("jobDate");

        jobName.setText(jobname);
        jobCharge.setText(jobcharge);
        jobLocation.setText(jobloc);
        //jobDate.setText(jobdate);

        Picasso.get().load(profpic).into(profPic);


    }
}

Followed by my adapter class:

    package com.example.oddsynew;

import android.content.Context;
import android.content.Intent;
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 com.squareup.picasso.Picasso;

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, final 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());
        holder.jobDate.setText(jobs.get(position).getJob_date());
        Picasso.get().load(jobs.get(position).getProf_pic()).into(holder.profPic);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, JobInfo.class);
                intent.putExtra("recruiterName",jobs.get(position).getRecruiter_name());
                intent.putExtra("jobName",jobs.get(position).getJob_name());
                intent.putExtra("jobCharge",jobs.get(position).getJob_charge());
                intent.putExtra("jobLocation",jobs.get(position).getLocation());
                intent.putExtra("profPic",jobs.get(position).getProf_pic());
                //intent.putExtra("jobDate",jobs.get(position).getJob_date());
                context.startActivity(intent);
            }
        });
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView recruiterName, jobName, jobLocation, jobCharge, jobDate;
        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);
            //jobDate = itemView.findViewById(R.id.jobDate_info);
        }
    }

}

And here is my model:

   package com.example.oddsynew;

public class Job {
    private String job_name, recruiter_name, location, job_charge, add_pref, job_date, start_time, end_time, tasks, job_desc, prof_pic;

    public Job() {
    }

    public Job(String job_name, String recruiter_name, String location, String job_charge, String add_pref, String job_date, String start_time, String end_time, String tasks, String job_desc, String prof_pic) {
        this.job_name = job_name;
        this.recruiter_name = recruiter_name;
        this.location = location;
        this.job_charge = job_charge;
        this.add_pref = add_pref;
        this.job_date = job_date;
        this.start_time = start_time;
        this.end_time = end_time;
        this.tasks = tasks;
        this.job_desc = job_desc;
        this.prof_pic = prof_pic;
    }

    public Job(String prof_pic) {
        this.prof_pic = prof_pic;
    }

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

    public String getProf_pic() {
        return prof_pic;
    }

    public void setProf_pic(String prof_pic) {
        this.prof_pic = prof_pic;
    }

    public String getAdd_pref() {
        return add_pref;
    }

    public void setAdd_pref(String add_pref) {
        this.add_pref = add_pref;
    }

    public String getJob_date() {
        return job_date;
    }

    public void setJob_date(String job_date) {
        this.job_date = job_date;
    }

    public String getStart_time() {
        return start_time;
    }

    public void setStart_time(String start_time) {
        this.start_time = start_time;
    }

    public String getEnd_time() {
        return end_time;
    }

    public void setEnd_time(String end_time) {
        this.end_time = end_time;
    }

    public String getTasks() {
        return tasks;
    }

    public void setTasks(String tasks) {
        this.tasks = tasks;
    }

    public String getJob_desc() {
        return job_desc;
    }

    public void setJob_desc(String job_desc) {
        this.job_desc = job_desc;
    }
}

My Job Info UI

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

    <TextView
        android:id="@+id/textView3"
        style="@style/HeaderStyle"
        android:layout_width="252dp"
        android:layout_height="39dp"
        android:layout_marginTop="24dp"
        android:fontFamily="@font/roboto_bold"
        android:text="Find Your Next Gig"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/imageButton"
        app:layout_constraintTop_toTopOf="parent" />

    <SearchView
        android:id="@+id/search"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="16dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="80dp"
        android:background="@drawable/rounded_button"
        android:iconifiedByDefault="false"
        android:paddingLeft="-10dp"
        android:queryBackground="@android:color/transparent"
        android:queryHint="Search Jobs"
        android:searchIcon="@drawable/ic_magnifying_glass"
        android:textColor="#fff"
        android:visibility="visible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3"
        app:theme="@style/Search" />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="29dp"
        android:background="@null"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_burger_button" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="30dp"
        android:layout_height="35dp"
        android:layout_marginTop="37dp"
        android:background="@null"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.4"
        app:layout_constraintStart_toEndOf="@+id/search"
        app:layout_constraintTop_toBottomOf="@+id/textView3"
        app:srcCompat="@drawable/ic_noun_filter" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="358dp"
        android:layout_height="0dp"
        android:layout_marginTop="20dp"
        android:background="#fff"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/search">

        <ImageView
            android:id="@+id/prof_pic_info"
            android:layout_width="110dp"
            android:layout_height="110dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="40dp"
            android:layout_marginBottom="10dp"
            android:scaleType="centerCrop"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.015" />

        <TextView
            android:id="@+id/jobName_info"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="40dp"
            android:layout_marginEnd="16dp"
            android:fontFamily="@font/roboto_bold"
            android:textColor="#000"
            android:textSize="20sp"
            android:textStyle="normal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toEndOf="@+id/prof_pic_info"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/jobLocation_info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="8dp"
            android:fontFamily="@font/roboto_light"
            android:textColor="#000"
            android:textSize="14sp"
            android:textStyle="normal"
            app:layout_constraintStart_toEndOf="@+id/prof_pic_info"
            app:layout_constraintTop_toBottomOf="@+id/jobName_info" />

        <TextView
            android:id="@+id/jobCharge_info"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:fontFamily="@font/roboto_light"
            android:textColor="#000"
            android:textSize="14sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toEndOf="@+id/jobLocation_info"
            app:layout_constraintTop_toBottomOf="@+id/jobName_info" />

        <TextView
            android:id="@+id/jobDate_info"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:fontFamily="@font/roboto"
            android:textColor="#000"
            android:textSize="14sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toEndOf="@+id/prof_pic_info"
            app:layout_constraintTop_toBottomOf="@+id/jobCharge_info" />

        <TextView
            android:id="@+id/jobTime_info"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:fontFamily="@font/roboto"
            android:textColor="#000"
            android:textSize="14sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toEndOf="@+id/prof_pic_info"
            app:layout_constraintTop_toBottomOf="@+id/jobDate_info" />

        <TextView
            android:id="@+id/jobDesc_header"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            android:fontFamily="@font/roboto_medium"
            android:textColor="#000"
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/prof_pic_info" />

        <TextView
            android:id="@+id/jobTasks_header"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            android:fontFamily="@font/roboto_medium"
            android:textColor="#000"
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/jobDesc" />

        <TextView
            android:id="@+id/additonalPref_header"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            android:fontFamily="@font/roboto_medium"
            android:textColor="#000"
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/jobTasks" />

        <TextView
            android:id="@+id/jobTasks"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:fontFamily="@font/roboto_medium"
            android:textColor="#000"
            android:textSize="14sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/jobTasks_header" />

        <TextView
            android:id="@+id/additionalPref"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:fontFamily="@font/roboto_medium"
            android:textColor="#000"
            android:textSize="14sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/additonalPref_header" />

        <TextView
            android:id="@+id/jobDesc"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:fontFamily="@font/roboto"
            android:textColor="#000"
            android:textSize="14sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/jobDesc_header" />

    </androidx.constraintlayout.widget.ConstraintLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

EDIT: As you can see here, it works when I retrieve this info, but when I try to retrieve job_date the same way. It crashes.

enter image description here

EDIT: For reference, this is my recycler list view:

enter image description here

1 Answers

I fixed the problem, all I had to do was remove holder for job_date. And it worked for some reason. Thanks to those who tried to help anyway.

package com.example.oddsynew;

import android.content.Context;
import android.content.Intent;
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 com.squareup.picasso.Picasso;

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 final MyViewHolder holder, final 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());
        //holder.jobDate.setText(jobs.get(position).getJob_date()); //Removed this 
        statement
        Picasso.get().load(jobs.get(position).getProf_pic()).into(holder.profPic);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try{
                Intent intent = new Intent(context, JobInfo.class);
                intent.putExtra("recruiterName",jobs.get(holder.getAdapterPosition()).getRecruiter_name());
                intent.putExtra("jobName",jobs.get(holder.getAdapterPosition()).getJob_name());
                intent.putExtra("jobCharge",jobs.get(holder.getAdapterPosition()).getJob_charge());
                intent.putExtra("jobLocation",jobs.get(holder.getAdapterPosition()).getLocation());
                intent.putExtra("profPic",jobs.get(holder.getAdapterPosition()).getProf_pic());
                intent.putExtra("jobDate",jobs.get(holder.getAdapterPosition()).getJob_date());

               context.startActivity(intent);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView recruiterName, jobName, jobLocation, jobCharge, jobDate;
        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);
        }
    }

}
Related