【问题标题】:How to show data with ListView in android?如何在 Android 中使用 ListView 显示数据?
【发布时间】:2016-12-07 11:53:42
【问题描述】:

我想显示来自服务器的数据。我的结果数据显示为 [get(0);指数(0),价格:3800]。我想在android中用listview显示所有数据。我使用 BaseAdapter、CustomAdapter。这里预订对象api。

[
  {
    "order_id": 13,
    "booking_person": 2,
    "booking_datetime": "2016-07-01 22:30:00",
    "total_amount": 2100,
    "order_details": [
      {
        "id": 20,
        "order_id": 13,
        "qty": 1,
        "price": 3800,
        "sub_total": 3800,
      },
      {
        "id": 21,
        "order_id": 13,
        "qty": 1,
        "price": 3250,
        "sub_total": 3250,,
      },
      {
        "id": 124,
        "order_id": 13,
        "qty": 1,
        "price": 2100,
        "sub_total": 2100,            
      }
    ]
  },
]

BookingActivity.java

Bundle bundle = getIntent().getExtras();
Booking booking = new Gson().fromJson(bundle.getString("Booking"), Booking.class);
List<Booking> bookingList = new ArrayList<Booking>();
bookingList.add(booking);
BookingDetailAdapter adapter = new BookingDetailAdapter(BookingActivity.this, bookingList);
lv.setAdapter(adapter);

BookingDetailAdapter.clsas

@Override
    public View getView(int position, View convertView, ViewGroup parent){
Booking booking = list.get(position);
        for (int i = 0; i < list.size(); i++) {
            holder.textView.setText(list.get(i).getOrderDetail().get(0).getPrice().toString());
        }
        return rowView;
}

【问题讨论】:

  • 您是否夸大了任何视图?
  • 查看 rowView rowView = inflater.inflate(R.layout.activity_listview, null);
  • 你能粘贴复杂的适配器类吗

标签: android api listview retrofit baseadapter


【解决方案1】:

首先,您应该使用 Gson 解析对象中的结果,然后将结果列表传递给 ListAdapter,然后在 getView 中:

@Override
public View getView(int position, View convertView, ViewGroup parent){

Booking booking = list.get(position);
    holder.textView.setText(list.get(position).getOrderDetail().get(0).getPrice().toString());

    return rowView;
}

【讨论】:

    【解决方案2】:

    您的适配器填充了您拥有的父 Booking 对象。但是如果您将order_detail 列表传递给适配器,您可以看到 3 行。

    BookingDetailAdapter adapter = new BookingDetailAdapter(BookingActivity.this, bookingList.get(0).getOrderDetail());
    

    您不应该在列表中循环。 getView 方法将为列表中的每个项目触发。

    OrderDetail detail = list.get(position);
    holder.textView.setText(detail.get(position).getPrice().toString());       
    return rowView;
    

    【讨论】:

    • 您的列表只有一项。 List&lt;Booking&gt; bookingList = new ArrayList&lt;Booking&gt;(); bookingList.add(booking); 你应该将order_details 列表传递给适配器。
    • detail.get(position) 不需要。非常感谢。我得到了它。 :)
    • 很高兴能为您提供帮助。如果可以,您可以投票或接受正确答案吗? :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多