【问题标题】:retrieve data on ListView from Firebase Realtime Database in android not working从 Android 中的 Firebase 实时数据库中检索 ListView 上的数据不起作用
【发布时间】:2023-03-15 12:33:01
【问题描述】:

我正在尝试从 firebase 动态检索数据并将它们显示在页面上,但是当我这样做时没有错误,但注意到屏幕上出现的只是一个空屏幕(见下图)

This is the output when I click on the page

谁能帮我解决这个问题?

这是我的Firebase 数据库结构。

这是我的java代码:

package com.example.atheer.booklyv1;

import android.app.ListActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

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

ListView ListView;
FirebaseDatabase database;
DatabaseReference ref;
ArrayList<Service> list;
ArrayAdapter<Service>  adapter;
Service ser;



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




        ser = new Service();
        ListView = (ListView) findViewById(R.id.ListView);
        database = FirebaseDatabase.getInstance();
        ref =database.getReference().child("client");
        list = new ArrayList<>();
        adapter = new ArrayAdapter<Service>(this, R.layout.service_info,R.id.serviceInfo,list);
        ref.addValueEventListener(new ValueEventListener(){


            @Override
            public void onDataChange(DataSnapshot dataSnapshot){
for(DataSnapshot ds: dataSnapshot.getChildren())
{
    if (dataSnapshot.hasChild("services")){
    ser= ds.getValue(Service.class);
    list.add(ser);}

}
ListView.setAdapter(adapter);
            }



            @Override
            public void onCancelled(DatabaseError databaseError){


            }
        });

    }




}

这是 ListView 的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".orgServices">
<ListView
    android:id="@+id/ListView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

</ListView>
</android.support.constraint.ConstraintLayout>

这是 ListView 中每个元素的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/serviceInfo"
        android:textSize="30sp"
        android:text="Name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</android.support.constraint.ConstraintLayout>

我自定义了适配器类: 适配器类:

package com.example.atheer.booklyv1;

import android.support.v7.app.AppCompatActivity;

public class Service extends AppCompatActivity {

private int price;
private String name;
private int totalpoint;
private int rating;


public Service(){

}

 public Service (String name){
    this.name=name;
 }

 public Service (String name, int price , int totalpoint , int rating ){
    this.name=name;
    this.price=price;
    this.totalpoint=totalpoint;
    this.rating=rating;


 }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getTotalpoint() {
        return totalpoint;
    }

    public void setTotalpoint(int totalpoint) {
        this.totalpoint = totalpoint;
    }

    public int getRating() {
        return rating;
    }

    public void setRating(int rating) {
        this.rating = rating;
    }
}

这些消息来自运行部分:

V/FA: Recording user engagement, ms: 748436
V/FA: Connecting to remote service
V/FA: Activity paused, time: 1449333
D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=748436, firebase_screen_class(_sc)=Service, firebase_screen_id(_si)=-4092656992746331939}]
I/atheer.booklyv: Waiting for a blocking GC ProfileSaver
V/FA: Connection attempt already in progress
V/FA: Connection attempt already in progress
      Activity resumed, time: 1449354
D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=Service, firebase_previous_id(_pi)=-4092656992746331939, firebase_screen_class(_sc)=Mynavigation, firebase_screen_id(_si)=-4092656992746331940}]
I/atheer.booklyv: WaitForGcToComplete blocked ProfileSaver on ProfileSaver for 62.701ms
V/FA: Connection attempt already in progress
D/EGL_emulation: eglMakeCurrent: 0xebd05420: ver 3 0 (tinfo 0xebd033e0)
D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 4
V/FA: Recording user engagement, ms: 3384
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@6a2bdcc
V/FA: Activity paused, time: 1452737
V/FA: onActivityCreated
D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=3384, firebase_screen_class(_sc)=Mynavigation, firebase_screen_id(_si)=-4092656992746331940}]
V/FA: Activity resumed, time: 1452998
D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=Mynavigation, firebase_previous_id(_pi)=-4092656992746331940, firebase_screen_class(_sc)=Service, firebase_screen_id(_si)=-4092656992746331938}]
D/EGL_emulation: eglMakeCurrent: 0xebd05420: ver 3 0 (tinfo 0xebd033e0)

【问题讨论】:

  • 这些日志没有帮助。相反,请尝试添加您的 json 或 The firebase 的数据库结构。
  • ListView ListView;使其成为 listVIew listView.setAdapter(adapter);
  • 我建议您从问题中删除一些不必要的代码和日志,因为较长的问题不会受到更多关注。

标签: java android firebase listview adapter


【解决方案1】:

要解决这个问题,只需移动以下代码行:

adapter = new ArrayAdapter<Service>(this, R.layout.service_info,R.id.serviceInfo,list);

就在之前:

ListView.setAdapter(adapter);

当你设置适配器时,你的列表是空的,这就是为什么屏幕是空的。

【讨论】:

  • 嗨瑞玛!你有没有试过我上面的解决方案,它有效吗?
【解决方案2】:

问题在于使用dataSnapshot 进行检索。您正在做的是,如果您找到一个名为 services 的孩子,您会尝试从 Services.class 中的当前 dataSnapshot 获取数据。

您应该做的是,在您选择的孩子上设置正确的databaseReference

因此,您可以在 if 语句中尝试此操作,而不是您用来检索的代码:

String name = dataSnapshot.child("name").getValue(String.class);
String name = dataSnapshot.child("price").getValue(Integer.class);
String name = dataSnapshot.child("rating").getValue(Integer.class);
String name = dataSnapshot.child("totalpoint").getValue(Integer.class);

如果您想获取所有值而不设置不同的getValue() 语句,您可以使用map 来执行此操作。这可以帮助您同时从数据库中更新和检索值。

您可以参考以下链接了解更多关于使用mapFirebaseDatabase 检索数据的信息。

Retrieving data using Map from Firebase Database

【讨论】:

    猜你喜欢
    • 2019-05-30
    • 1970-01-01
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 2020-07-15
    • 2019-04-16
    • 2020-12-09
    • 2021-05-21
    相关资源
    最近更新 更多