【问题标题】:Data fetched from Firebase Realtime Database is not displayed in my RecyclerView从 Firebase 实时数据库获取的数据未显示在我的 RecyclerView 中
【发布时间】:2020-12-07 20:19:58
【问题描述】:

我正在尝试显示我的 firebase 实时数据库中的项目,我的数据库中有两个节点,我在适配器类 getItemCount() 函数上使用了 Log 方法,它显示有 2 个,但它不是t 显示在我的 UI 中,两个项目的视图正在生成,但没有显示名称和暴民的内容。

Output in AVD Database Screenshot

主要功能代码

package com.example.jamsecure;

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

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;
import java.util.List;

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

public class User_Select_Jam_Studio extends AppCompatActivity {

    List<FetchData> fetchData;
    RecyclerView recyclerView;
    HelperAdapter helperAdapter;
    DatabaseReference databaseReference;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user__select__jam__studio);
        recyclerView=findViewById(R.id.recyclerview);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager((new LinearLayoutManager(this)));
        fetchData=new ArrayList<>();
        helperAdapter=new HelperAdapter(fetchData);
        recyclerView.setAdapter(helperAdapter);


        databaseReference= FirebaseDatabase.getInstance().getReference("Owners");

        databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot ds:dataSnapshot.getChildren())
                {
                    FetchData data=ds.getValue(FetchData.class);
                    fetchData.add(data);
                    helperAdapter.notifyDataSetChanged();

                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Toast.makeText(User_Select_Jam_Studio.this,"Data base problem",Toast.LENGTH_LONG).show();
            }
        });

    }
}  

适配器类

package com.example.jamsecure;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

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

public class HelperAdapter extends RecyclerView.Adapter {
    List<FetchData> fetchDataList;

    public HelperAdapter(List<FetchData> fetchDataList) {
        this.fetchDataList = fetchDataList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.jamrooms,parent, false);
        ViewHolderClass viewHolderClass = new ViewHolderClass(view);
        return viewHolderClass;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    ViewHolderClass viewHolderClass=(ViewHolderClass)holder;
        FetchData fetchData=fetchDataList.get(position);
        viewHolderClass.name.setText(fetchData.getName());
        viewHolderClass.jamr.setText(fetchData.getJamrate());
        viewHolderClass.mob.setText(fetchData.getMob());

     }

    @Override
    public int getItemCount() {
        int a =fetchDataList.size();
        String b=Integer.toString(a);
        Log.d("TAG",b);
        return fetchDataList.size();
    }
    public class ViewHolderClass extends RecyclerView.ViewHolder{
        TextView name,jamr,mob;
        public ViewHolderClass(@NonNull View itemView) {
            super(itemView);
            name=itemView.findViewById(R.id.name);
            jamr=itemView.findViewById(R.id.jamrate);
            mob=itemView.findViewById(R.id.mob);
        }
    }
}

获取数据类

package com.example.jamsecure;

public class FetchData {
    String sname;
    String jam_rate;
    String phone;
    public FetchData(){}
    public FetchData( String sname, String jam_rate, String phone) {
        this.sname = sname;
        this.jam_rate = jam_rate;
        this.phone = phone;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public void setJam_rate(String jam_rate) {
        this.jam_rate = jam_rate;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getName() {
        return sname;
    }

    public String getJamrate() {
        return jam_rate;
    }

    public String getMob() {
        return phone;
    }

}

充气机活动

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/item_layout"
    android:orientation="vertical"
    android:visibility="visible"
    tools:visibility="visible">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/name"
        android:textColor="#000000"
        android:textSize="40sp" />

    <TextView
        android:id="@+id/mob"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/mob"
        android:textColor="#000000"
        android:textSize="40sp" />

    <TextView
        android:id="@+id/jamrate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/jam_rate"
        android:textColor="#000000"
        android:textSize="40sp" />


</LinearLayout>

回收站查看活动

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:background="@drawable/back6"
    tools:context=".User_Select_Jam_Studio">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

【问题讨论】:

  • 请编辑您的问题并将您的数据库结构添加为 JSON 文件或至少是屏幕截图。
  • @AlexMamo 是的先生,我已经从我的 firebase 控制台添加了数据库截图。

标签: java android firebase firebase-realtime-database


【解决方案1】:

您的代码的问题在于“FetchData”类中的 getter 名称不正确。例如,“电话”字段的 getter 应该是 getPhone() 和 not getMob()。所以你的“FetchData”类应该是这样的:

public class FetchData {
    private String jam_rate, location, phone, semail, sname;

    public FetchData() {}

    public FetchData(String jam_rate, String location, String phone, String semail, String sname) {
        this.jam_rate = jam_rate;
        this.location = location;
        this.phone = phone;
        this.semail = semail;
        this.sname = sname;
    }

    public String getJam_rate() {
        return jam_rate;
    }

    public void setJam_rate(String jam_rate) {
        this.jam_rate = jam_rate;
    }

    public String getLocation() {
        return location;
    }

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

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getSemail() {
        return semail;
    }

    public void setSemail(String semail) {
        this.semail = semail;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }
}

请注意,setter 和 getter 不是必需的。 Setter 始终是可选的,因为如果 JSON 属性没有 setter,Firebase 客户端会将值直接设置到字段中。如果您公开这些字段,则 getter 也是可选的。因此,您的班级的更简化版本可能如下所示:

public class FetchData {
    public String jam_rate, location, phone, semail, sname;

    public FetchData() {}

    public FetchData(String jam_rate, String location, String phone, String semail, String sname) {
        this.jam_rate = jam_rate;
        this.location = location;
        this.phone = phone;
        this.semail = semail;
        this.sname = sname;
    }
}

【讨论】:

    猜你喜欢
    • 2021-01-15
    • 1970-01-01
    • 2018-11-15
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    相关资源
    最近更新 更多