【问题标题】:My RecyclerView is not showing any data - Firebase我的 RecyclerView 没有显示任何数据 - Firebase
【发布时间】:2020-03-08 17:41:59
【问题描述】:

我正在关注 youtube 上的教程并尝试将 firebase 数据显示为回收站视图。但是,它在我的布局中完全是空白的。我不明白为什么。我在这里遇到了类似的问题并尝试了所有解决方案,但都没有奏效。请帮我解决这个问题。谢谢。

以下是我的 Firebase 数据库。

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

这是我的布局

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

这是模型类

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

还有我的Home Adapter 类

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

【问题讨论】:

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


    【解决方案1】:
    1. 需要在for循环后调用adapter.notifyDataSetChanged()通知适配器将数据源(列表)中更新后的数据重新处理为

      for (DataSnapshot ds: dataSnapshot.getChildren()){
          Job j = ds.getValue(Job.class);
          list.add(j);
      }
      adapter.notifyDataSetChanged();
      // ^^^^^^^^^^^^^^^^^^^^^^^
      
    2. 要获取孩子,请使用Jobs 而不是Job as

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

    【讨论】:

    • 嗨,谢谢。我添加了它,但仍然没有任何显示。
    • @Devika.S 不用谢我,你能收到数据吗,检查list的大小
    • System.out: 0 显然它没有接收数据。不知道我哪里出错了。
    • @Devika.S 知道了,您需要使用job1 作为密钥或jobs .child("Jobs") 因为没有job 密钥
    • 很高兴能帮上忙,编码愉快!
    【解决方案2】:

    替换

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

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

    并添加 adapter.notifyDataSetChanged() 如下

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-17
      相关资源
      最近更新 更多