【问题标题】:Populating List<Service> into android ListView giving different value将 List<Service> 填充到 android ListView 中,给出不同的值
【发布时间】:2016-12-08 06:57:12
【问题描述】:

我有一个从getParcelableArrayListExtra() 收到的List&lt;Service&gt; 现在我需要将收到的对象填充到一个 android ListView 中

我尝试通过以下方式填充 ListView。填充 ListView 以显示对象引用 ID。而不是显示内容。

如何显示此列表中的内容。

实现如下:

List<Service> serviceCart = getIntent().getParcelableArrayListExtra("serviceCart");

ListView listView = (ListView) findViewById(R.id.lv_choosed_services);

ArrayAdapter<Service> serviceArrayAdapter = new ArrayAdapter<Service>(this,android.R.layout.simple_list_item_1,serviceCart);

listView.setAdapter(serviceArrayAdapter);

【问题讨论】:

  • serviceCart 在哪里?
  • List serviceCart = getIntent().getParcelableArrayListExtra("serviceCart");
  • serviceCart 是一个 List 的 Service 对象
  • 你有没有放入Service类:公共类Service实现Parcelable?

标签: android listview


【解决方案1】:

打开ArrayAdapter并从第405行读取到第410行

final T item = getItem(position);
if (item instanceof CharSequence) {
    text.setText((CharSequence) item);
} else {
    text.setText(item.toString());
}

你在做什么:

如果您的项目是String Adpater.getView(params) 将使用它,否则它将使用item.toString()。在您的Service.java 中,您没有toString(),因此应用程序使用默认默认toString()

你应该做什么:

  • 最好创建自定义 ArrayAdapter 并填充您的视图
  • 在您的Service.java 中覆盖toString() 并仅使用您希望在适配器中填充的那些值,例如service_name 和/或service_code,其余的将由默认实现完成。

注意: 请阅读Naming Conventions,不建议将您的对象命名为service_name,应为serviceName

【讨论】:

    猜你喜欢
    • 2015-06-28
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多