【问题标题】:Data from firebase not showing up onclick recyclerview来自firebase的数据未显示onclick recyclerview
【发布时间】:2020-06-25 15:07:21
【问题描述】:

我有一个回收商列表视图,当单击一个时,我想打开一个包含其信息的新页面。当我尝试在新页面中显示工作名称、位置和工作费用时,它会显示出来。但是,当我尝试显示 job_date 时。我的应用程序崩溃。我不知道为什么,因为我使用相同的方法来显示数据。当我注释掉 job_date 出现的任何地方时,它都可以正常工作。

编辑:我想通过崩溃来澄清一下,我的意思是它不会立即关闭。它一直有效,直到我到达列表视图页面,然后它突然回到我的主要活动页面,该页面只有一个按钮可以进入下一页。

我的 logcat 中只有这两个 错误,但我不确定它是否与问题有关:

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

我有一个 Firebase 数据库,如下所示:

这是我的主要家庭课程

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

这是我的工作信息类

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


    }
}

接着是我的适配器类

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

}

这是我的模型

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

我的工作信息用户界面

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

编辑:正如您在此处看到的,当我检索此信息时它可以工作,但是当我尝试以相同的方式检索 job_date 时。它崩溃了。

编辑:作为参考,这是我的回收站列表视图:

【问题讨论】:

  • 如果应用程序崩溃,会有一个堆栈跟踪。请在 logcat 上查找,并将其添加到您的问题中。
  • @AlexMamo 我已经提到了我在帖子中遇到的错误。
  • 你能分享错误日志吗?你如何将这个工作对象保存到实时数据库中?我认为最重要的部分是两者。
  • 请分享完整的错误日志,你说的还不够
  • 您的 job_row 布局文件是否与 activity_job_info 布局文件中的字段名称相同?

标签: android firebase-realtime-database android-recyclerview


【解决方案1】:

我解决了这个问题,我所要做的就是删除 job_date 的持有人。它出于某种原因起作用。感谢那些试图提供帮助的人。

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

}

【讨论】:

    猜你喜欢
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    相关资源
    最近更新 更多