我正在尝试从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)
答案 0 :(得分:0)
问题在于使用dataSnapshot
进行检索。您正在做的是,如果找到一个名为services
的孩子,则尝试从dataSnapshot
中的当前Services.class
获取数据。
您应该做的是,对您选择的孩子设置正确的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
来执行。这可以帮助您同时从数据库中更新和检索值。
您可以参考以下链接以了解有关使用FirebaseDatabase
从map
进行数据检索的更多信息。
答案 1 :(得分:0)
要解决此问题,只需移动以下代码行:
adapter = new ArrayAdapter<Service>(this, R.layout.service_info,R.id.serviceInfo,list);
之前:
ListView.setAdapter(adapter);
在设置适配器时,列表为空,这就是为什么屏幕为空。