【问题标题】:firebase retrieving value not correctfirebase 检索值不正确
【发布时间】: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


【解决方案1】:

您在LisView 中得到的基本上是您的Product 对象的字符串表示形式。所以下面的字符串:

 com.example.mark.mobilethesis.Product@e8e0966

实际上代表Product对象从内存中的地址,这肯定不是你想要的。要显示description 和qty 这两个数据,您有三个选项。

第一个也是最简单的一个是在您的Product 类中覆盖toString() 方法,其他用户也建议这样做。这应该是这样的:

@Override
public String toString() {
    return description + " / " + qty;
}

第二种选择是将列表的类型从Product 更改为String,如下所示:

for (DataSnapshot child : children) {
    List<String> products = new ArrayList<>();
    Product value = child.getValue(Product.class);
    products.add(value.getDescription() + " / " + child.getValue());
}

ArrayAdapter<String> productAdapter = new ArrayAdapter<String>(
    getApplicationContext(),
    android.R.layout.simple_list_item_1,
    products
);
listViewProducts.setAdapter(productAdapter);

第三个是创建自定义适配器。因此,即使您将整个 Product 对象传递给适配器,您也只能获得适配器内部的 description 和 qty。

【讨论】:

  • 嗨,马克!你有没有试过我上面的解决方案,它有效吗?
  • 感谢您的帮助。问题解决了。我做了一个自定义适配器,它现在可以工作了。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多