【发布时间】:2018-09-28 05:42:08
【问题描述】:
我在 Firebase 中检索值时遇到问题。我添加了一个TextView 以查看数据快照中的值,并且我正确地看到了它,但是当我将它添加到我的ListView 中时,它看起来像这样。顺便说一句,我的ListView 是绿色的。
在我的TextView 中,该值是正确的,但在我的列表视图中是这样的。这是我用于检索值的代码。
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference();
ref.child("Sold").child("Item").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
try {
Iterable<DataSnapshot> children = dataSnapshot.getChildren();
//textView.setText(dataSnapshot.getValue().toString());
for (DataSnapshot child : children) {
Product value = child.getValue(Product.class);
products.add(value);
textView.setText(value.getDescription() + " - " + child.getValue());
}
productAdapter.notifyDataSetChanged();
}catch (Exception e)
{
textView.setText(e.getMessage());
}
}
productAdapter = new ArrayAdapter<Product>(getApplicationContext(),
android.R.layout.simple_list_item_1, products);
listViewProducts.setAdapter(productAdapter);
这是我的产品类
class Product {
private String Description;
private int Qty;
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public int getQty() {
return Qty;
}
public void setQty(int qty) {
Qty = qty;
}
public Product(String description, int qty) {
Description = description;
Qty = qty;
}
public Product(){}
}
【问题讨论】:
-
尝试在你的 Product 类中实现 toString
-
@Solace 我在问题中添加了我的产品类。我应该把 toString() 放在哪里?
-
在 Product 类中。返回一个您想要表示产品的字符串,这应该是您在 ListView 中看到的每个产品的内容
-
@Solace 还是一样的 :(
-
@Mark 您正在将整个对象设置为这一行列表的文本视图 -> products.add(value);如果您想在适配器中传递类,您应该使用 viewHolder 方法或将此 products.add(value) 更改为 products(value.getDescription()) 等
标签: java android firebase listview firebase-realtime-database