gpt4 book ai didi

android - Firebase 不向 ListView 显示数据

转载 作者:行者123 更新时间:2023-11-29 02:35:38 26 4
gpt4 key购买 nike

我正在尝试为大学类(class)制作一个 Android 应用程序,我需要显示来自 firebase 数据库的信息并将其显示在 ListView 中。我进行了很多搜索,但找不到任何可以帮助我的东西。我需要帮助。该应用程序向我发送了某种类型的错误,但我无法找出问题所在。我现在需要获取产品的每个名称并将其显示在 ListView 中。顺便说一句,我是 Android Studio 的新手。

This my database of firebase

这是我的 Activity :

public class buscador extends AppCompatActivity {

ListView lv;
private DatabaseReference myRef,ref,ref2;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListner;
private String id;
private FirebaseDatabase mFirebaseDatabase;
private static final String TAG = "buscador";
int num=1;
private ArrayList<String> array2;

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

lv=(ListView)findViewById(R.id.cat1);
EditText search = (EditText)findViewById(R.id.searchfind);
mAuth=FirebaseAuth.getInstance();
FirebaseUser user=mAuth.getCurrentUser();
id=ref.getKey();

setupFirebaseAuth();


ref=FirebaseDatabase.getInstance().getReference("Products");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
showData(dataSnapshot);
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}
public void showData(DataSnapshot dataSnapshot){
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Producto info = new Producto();
info.setName(ds.child(id).getValue(Producto.class).getName());

Log.d(TAG, "showData:name:" + info.getName());
ArrayList<String> array=new ArrayList<>();
array.add(info.getName());
ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,array);
lv.setAdapter(adapter);}
}

Producto 类对象:

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

错误:

  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)

最佳答案

要从您的 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);

你的输出将是:

ps4
Xbox
//and so on

关于android - Firebase 不向 ListView 显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47300965/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com