简体   繁体   中英

retrieve data on ListView from Firebase Realtime Database in android not working

I'm trying to retrieve data from firebase dynamically and display them on the page but when I do that there are no errors but noting appear on the screen just an empty screen (see the figure below )

This is the output when I click on the page

can anyone help me with that?

This is my Firebase database structure.

我的数据库结构

This is my java code :

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


            }
        });

    }




}

This is XML code for ListView :

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

This is XML code for each element in ListView :

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

I customized the adapter class : The adapter class :

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

and these messages come from the run section:

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)

The problem is in the retrieval using the dataSnapshot . What you're doing is that if you find a child with the name services you try to get the data from the current dataSnapshot in the Services.class .

What you should be doing is that, setting correct databaseReference on the child of your choice.

So instead of the code you're using to retrieve, you can try this inside your if statement:

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

If you want to however get all values without setting different getValue() statements, you may use map to do so. This can help you update and retrieve values simultaneously from your database.

You can refer the following link to know more about data retrieval from FirebaseDatabase using map .

Retrieving data using Map from Firebase Database

To solve this, simply move the following line of code:

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

right before:

ListView.setAdapter(adapter);

By the time you are setting the adapter, you list is empty, that's why are getting an empty screen.

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