简体   繁体   中英

Firebase data retrieve and passing it from fragment to new activity not working

I am developing an android app which shows the user with the latest jobs from various fields. When the jobs are displayed and when the user clicks on view more, the job description is not getting displayed.

I have passed data from fragment to new activity and displaying it in the new activity. There is no error but the details are not getting displayed. I have attached the code and the firebase database structure.

Here is the WorkFragment.java file which shows the jobs in recyclerview.

WorkFragment.java

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
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 com.google.firebase.database.annotations.Nullable;

import java.util.ArrayList;

public class WorkFragment extends Fragment {
    DatabaseReference databaseReference;
    RecyclerView recyclerView;
    FirebaseDatabase firebaseDatabase;
    ArrayList<Jobs> jobs;
    MyAdapter adapter;
    ProgressDialog pd;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_work, container, false);
        recyclerView = view.findViewById(R.id.work);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        pd = new ProgressDialog(getContext());
        pd.setMessage("Loading Jobs for you...");
        pd.show();
        jobs = new ArrayList<Jobs>();
        firebaseDatabase = FirebaseDatabase.getInstance();
        databaseReference = firebaseDatabase.getReference().child("Jobs");
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot ds: dataSnapshot.getChildren()){
                    Jobs j = ds.getValue(Jobs.class);
                    jobs.add(j);
                }
                pd.dismiss();
                adapter = new MyAdapter(getContext(), jobs);
                recyclerView.setAdapter(adapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                pd.dismiss();
            }
        });
        return view;
    }
}

Here is the MyAdapter.java class which does the job for recyclerview.

MyAdapter.java

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

import androidx.annotation.NonNull;
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 MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    Context context;
    ArrayList<Jobs> jobs;
    FirebaseDatabase firebaseDatabase;
    DatabaseReference databaseReference;

    public MyAdapter(Context c, ArrayList<Jobs> 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, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.jobtitle.setText(jobs.get(position).getJobtitle());
        holder.jobtype.setText(jobs.get(position).getJobtype());
        holder.companyname.setText(jobs.get(position).getCompany());
        holder.location.setText(jobs.get(position).getLocation());
        holder.jobdate.setText(jobs.get(position).getJobdate());
        holder.viewmore.setText(jobs.get(position).getViewmore());
        firebaseDatabase = FirebaseDatabase.getInstance();
        databaseReference = firebaseDatabase.getReference("Jobs");
        holder.viewmore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                databaseReference.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        String jobtitle = dataSnapshot.child("jobtitle").getValue(String.class);
                        String company = dataSnapshot.child("company").getValue(String.class);
                        String location = dataSnapshot.child("location").getValue(String.class);
                        String jobtype = dataSnapshot.child("jobtype").getValue(String.class);
                        String startDate = dataSnapshot.child("startdate").getValue(String.class);
                        String salary = dataSnapshot.child("salary").getValue(String.class);
                        String about = dataSnapshot.child("about").getValue(String.class);
                        String responsiblities = dataSnapshot.child("responsiblities").getValue(String.class);
                        String musthave = dataSnapshot.child("musthave").getValue(String.class);
                        String seniority = dataSnapshot.child("seniority").getValue(String.class);

                        Intent intent = new Intent(context, JobDescription.class);
                        intent.putExtra("JOBTITLE", jobtitle);
                        intent.putExtra("COMPANY", company);
                        intent.putExtra("LOCATION", location);
                        intent.putExtra("JOBTYPE", jobtype);
                        intent.putExtra("STARTDATE", startDate);
                        intent.putExtra("SALARY", salary);
                        intent.putExtra("ABOUT", about);
                        intent.putExtra("RESPONSIBLITIES", responsiblities);
                        intent.putExtra("MUSTHAVE", musthave);
                        intent.putExtra("SENIORITY", seniority);
                        context.startActivity(intent);
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });
            }
        });
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder{

        TextView jobtitle, jobtype, companyname, location, jobdate, viewmore;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            jobtitle = (TextView) itemView.findViewById(R.id.jobTitle);
            jobtype = (TextView) itemView.findViewById(R.id.jobType);
            companyname = (TextView) itemView.findViewById(R.id.companyName);
            location = (TextView) itemView.findViewById(R.id.location);
            jobdate = (TextView) itemView.findViewById(R.id.jobDate);
            viewmore = (TextView) itemView.findViewById(R.id.viewMore);
        }
    }
}

Here is the Job.class.

Jobs.java

    public class Jobs {
    private String jobtitle, jobtype, company, location, jobdate, viewmore, startdate, salary, about, responsiblities, musthave, seniority;

    public Jobs() {
    }

    public Jobs(String jobtitle, String jobtype, String company, String location, String jobdate, String viewmore, String startdate, String salary, String about, String responsiblities, String musthave, String seniority) {
        this.jobtitle = jobtitle;
        this.jobtype = jobtype;
        this.company = company;
        this.location = location;
        this.jobdate = jobdate;
        this.viewmore = viewmore;
        this.startdate = startdate;
        this.salary = salary;
        this.about = about;
        this.responsiblities = responsiblities;
        this.musthave = musthave;
        this.seniority = seniority;
    }

    public String getJobtitle() {
        return jobtitle;
    }

    public void setJobtitle(String jobtitle) {
        this.jobtitle = jobtitle;
    }

    public String getJobtype() {
        return jobtype;
    }

    public void setJobtype(String jobtype) {
        this.jobtype = jobtype;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getLocation() {
        return location;
    }

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

    public String getJobdate() {
        return jobdate;
    }

    public void setJobdate(String jobdate) {
        this.jobdate = jobdate;
    }

    public String getViewmore() {
        return viewmore;
    }

    public void setViewmore(String viewmore) {
        this.viewmore = viewmore;
    }

    public String getStartdate() {
        return startdate;
    }

    public void setStartdate(String startdate) {
        this.startdate = startdate;
    }

    public String getSalary() {
        return salary;
    }

    public void setSalary(String salary) {
        this.salary = salary;
    }

    public String getAbout() {
        return about;
    }

    public void setAbout(String about) {
        this.about = about;
    }

    public String getResponsiblities() {
        return responsiblities;
    }

    public void setResponsiblities(String responsiblities) {
        this.responsiblities = responsiblities;
    }

    public String getMusthave() {
        return musthave;
    }

    public void setMusthave(String musthave) {
        this.musthave = musthave;
    }

    public String getSeniority() {
        return seniority;
    }

    public void setSeniority(String seniority) {
        this.seniority = seniority;
    }
}

Here is the JobDescription.java which shows the details of job. In this class, Iam getting details from fragment and displaying it.

JobDescription.java

import androidx.appcompat.app.AppCompatActivity;

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


public class JobDescription extends AppCompatActivity {

    TextView jobtitle, company, location, startDate, salary, aboutText, responsiblityText, musthaveText, jobtypetext, seniority;
    Button applyNow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_job_description);
        getSupportActionBar().setTitle("Job Description");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        jobtitle = (TextView) findViewById(R.id.jobTitle);
        company = (TextView) findViewById(R.id.company);
        location = (TextView) findViewById(R.id.location);
        startDate = (TextView) findViewById(R.id.startDate);
        salary = (TextView) findViewById(R.id.salary);
        aboutText = (TextView) findViewById(R.id.aboutText);
        responsiblityText = (TextView) findViewById(R.id.responsiblityText);
        musthaveText = (TextView) findViewById(R.id.musthaveText);
        jobtypetext = (TextView) findViewById(R.id.jobtypetext);
        seniority = (TextView) findViewById(R.id.seniority);
        applyNow = (Button) findViewById(R.id.applyBtn);

        jobtitle.setText(getIntent().getStringExtra("JOBTITLE"));
        company.setText(getIntent().getStringExtra("COMPANY"));
        location.setText(getIntent().getStringExtra("LOCATION"));
        startDate.setText(getIntent().getStringExtra("STARTDATE"));
        salary.setText(getIntent().getStringExtra("SALARY"));
        aboutText.setText(getIntent().getStringExtra("ABOUT"));
        responsiblityText.setText(getIntent().getStringExtra("RESPONSIBLITIES"));
        musthaveText.setText(getIntent().getStringExtra("MUSTHAVE"));
        jobtypetext.setText(getIntent().getStringExtra("JOBTYPE"));
        seniority.setText(getIntent().getStringExtra("SENIORITY"));
    }
}

Here is the firebase database structure.

火力地堡数据库

In your adapter you dont need to add valueeventlistner, all you need is to pass data to descriptionactivity as shown below

 holder.viewmore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                        Intent intent = new Intent(context, JobDescription.class);
                        intent.putExtra("JOBTITLE", jobs.getJobtitle);
                        intent.putExtra("COMPANY", jobs.getcompany);

                        context.startActivity(intent);
                    }


        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM