【发布时间】: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