Firebase未向列表视图显示数据

时间:2017-11-15 06:42:42

标签: android listview firebase firebase-realtime-database

我正在尝试为大学课程制作一个Android应用程序,我需要显示来自firebase数据库的信息并在列表视图中显示它。我搜索了很多,但找不到任何可以帮助我的东西。我需要帮助。应用程序给我发了一些错误,但我找不到有什么问题。我需要获取产品的每个名称,并将其显示在列表视图中。 顺便说一下,我是Android Studio的新手。

System.Speech.Synthesis hangs with high CPU on 2012 R2

这是我的活动:

public class Producto {
private String image;
private String name;
private float price;
private String descripcion;
private String catalogo;
private String id;

public Producto(String image, String name, float price, String descripcion, String catalogo, String id) {
    this.image = image;
    this.name = name;
    this.price = price;
    this.descripcion = descripcion;
    this.catalogo = catalogo;
    this.id = id;
}

public Producto() {
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public String getName() {
    return name;
}

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

public float getPrice() {
    return price;
}

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

public String getDescripcion() {
    return descripcion;
}

public void setDescripcion(String descripcion) {
    this.descripcion = descripcion;
}

public String getCatalogo() {
    return catalogo;
}

public void setCatalogo(String catalogo) {
    this.catalogo = catalogo;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}
    }

Producto类对象:

  Process: com.example.roge.navapp, PID: 32020

java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.example.roge.navapp/com.example.roge.navapp.buscador}: 
java.lang.NullPointerException                                                                             
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)                                                                             
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)                                                                             
at android.app.ActivityThread.access$800(ActivityThread.java:166)                                                                             
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)                                                                             
at android.os.Handler.dispatchMessage(Handler.java:102)                                                                             
at android.os.Looper.loop(Looper.java:136)                                                                             
at android.app.ActivityThread.main(ActivityThread.java:5590)                                                                             
at java.lang.reflect.Method.invokeNative(Native Method)                                                                             
at java.lang.reflect.Method.invoke(Method.java:515)                                                                             
at Com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)                                                                             
at dalvik.system.NativeStart.main(Native Method)                                                                          
Caused by: java.lang.NullPointerException                                                                             
at com.example.roge.navapp.buscador.onCreate(buscador.java:51)                                                                             
at android.app.Activity.performCreate(Activity.java:5447)                                                                             
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)

错误:

Enum.chunk_every([1, 2, 3, 4, 5, 6], 2)

1 个答案:

答案 0 :(得分:0)

要从Firebase数据库中取出name,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference productsRef = rootRef.child("Products");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<String> list = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String name = ds.child("name").getValue(String.class);
            list.add(name);
            Log.d("TAG", name);
        }
        Log.d("TAG", list);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
productsRef.addListenerForSingleValueEvent(eventListener);

Yout输出将是:

ps4
Xbox
//and so on