求解,listView里面嵌套listView数据显示混乱

boatwater 应用层 2015-08-11 02:58:21
我弄了个listView嵌套listView 从服务器上获取数据
最后显示出来,子项listView 把所有orderproduct里的数据都显示出来了,

Json格式大致是这样
{
"code": "1",
"result": [
{
"id": "10259",
"createdate": "2014-06-30 21:23",
"amount": "95.00",
"score": "0",
"paytype": "1",
"statu": "已取消",
"iscomment": "null",
"orderproduct": {
"picture": "/attached/image/20140120/1390185473547_3.jpg",
"productid": "10308",
"productname": "钢铁是怎样炼成的",
"productnumber": "1",
"productprice": "90.0"
}
},
{
"id": "10228",
"createdate": "2014-03-04 17:50",
"amount": "1085.00",
"score": "0",
"paytype": "1",
"statu": "已取消",
"iscomment": "null",
"orderproduct": [
{
"picture": "/attached/image/20140304/1393898360387_3.jpg",
"productid": "10262",
"productname": "儿童 新生婴儿纯棉防水围脖 围嘴 口水巾 宝宝超可爱绣花围兜围嘴",
"productnumber": "1",
"productprice": "12.0"
},
{
"picture": "/attached/image/20140304/1393898360387_3.jpg",
"productid": "10269",
"productname": "玛尚N6000 USB笔记本电脑有线鼠标 女生可爱鼠标有线 USB鼠标",
"productnumber": "1",
"productprice": "16.0"
}
]
}
]
}

当我单独读取订单号"id": "10228",这条数据时显示是正常的
下面的是Json解析的类
public static void orderListRead(String data, List<Info> list,
Context context) {
Info info;
OrderChildInfo childInfo;
OrderChildAdapter childAdapter;
List<OrderChildInfo> childList = new ArrayList<OrderChildInfo>();

try {
JSONObject jsonobject = new JSONObject(data);
JSONArray array = jsonobject.getJSONArray("result");
for (int i = 0; i < array.length(); i++) {
info = new Info();
JSONObject object = array.getJSONObject(i);
String id = object.optString("id");// 订单号
String time = object.optString("createdate");// 时间
String score = object.optString("score");// 积分
String status = object.optString("statu");// 状态
String amount = object.optString("amount");// 合计
JSONArray jsonArray = object.getJSONArray("orderproduct");
for (int j = 0; j < jsonArray.length(); j++) {
childInfo = new OrderChildInfo();
JSONObject objects = jsonArray.getJSONObject(j);
String name = objects.optString("productname");// 商品
String number = objects.optString("productnumber");// 数量
String price = objects.optString("productprice");// 价格
childInfo.setOrderChildName(name);
childInfo.setOrderChildCount(number);
childInfo.setOrderChildPrice(price);
childList.add(childInfo);
}
childAdapter = new OrderChildAdapter(context, childList);
info.setOderCode("订单号" + id);
info.setOderTime("时间:" + time);
info.setOderIntegral("积分" + score);
info.setOderPrice("合计" + amount);
info.setOderStatus(status);
info.setChildAdapter(childAdapter);
list.add(info);

}

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

下面是adapter
public class OrderListAdapter extends BaseAdapter {
List<Info> list;
LayoutInflater inflater;
Context context;

public OrderListAdapter(Context context, List<Info> list) {
this.context = context;
this.list = list;
inflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return list == null ? 0 : list.size();
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int position, View view, ViewGroup arg2) {
// TODO Auto-generated method stub
OrderHolder holder;
if (view == null) {
holder = new OrderHolder();
view = inflater.inflate(R.layout.order_list_parent_item, null);
holder.orderCode = (TextView) view
.findViewById(R.id.order_item_code);
holder.orderStatus = (TextView) view
.findViewById(R.id.order_item_status);
holder.orderTime = (TextView) view
.findViewById(R.id.order_item_time);
holder.orderPrice = (TextView) view
.findViewById(R.id.order_item_price);
holder.orderIntegral = (TextView) view
.findViewById(R.id.order_item_integral);
holder.oderChildList = (ListView) view
.findViewById(R.id.order_item_list);
view.setTag(holder);
} else {
holder = (OrderHolder) view.getTag();
}
Info info = list.get(position);
holder.orderCode.setText(info.getOderCode());
holder.orderStatus.setText(info.getOderStatus());
holder.orderTime.setText(info.getOderTime());
holder.orderPrice.setText(info.getOderPrice());
holder.orderIntegral.setText(info.getOderIntegral());
holder.oderChildList.setAdapter(info.getChildAdapter());

return view;
}

class OrderHolder {
TextView orderCode = null;
TextView orderStatus = null;
TextView orderTime = null;
TextView orderPrice = null;
TextView orderIntegral = null;
ListView oderChildList = null;

}
下面是子项listView的adapter
public class OrderChildAdapter extends BaseAdapter {
List<OrderChildInfo> list;
LayoutInflater inflater;
Context context;

public OrderChildAdapter(Context context, List<OrderChildInfo> list) {
this.context = context;
this.list = list;
inflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return list == null ? 0 : list.size();
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int position, View view, ViewGroup arg2) {
// TODO Auto-generated method stub
OrderChildHolder holder;
if (view == null) {
holder = new OrderChildHolder();
view = inflater.inflate(R.layout.order_list_child_item, null);
holder.orderIcon = (ImageView) view
.findViewById(R.id.order_list_child_img);
holder.orderName = (TextView) view
.findViewById(R.id.order_list_child_name);
holder.orderCount = (TextView) view
.findViewById(R.id.order_list_child_count);
holder.orderPrice = (TextView) view
.findViewById(R.id.order_list_child_price);
view.setTag(holder);
} else {
holder = (OrderChildHolder) view.getTag();
}
OrderChildInfo info = list.get(position);
BitmapUtils bitmapUtils = new BitmapUtils(context);
bitmapUtils.display(holder.orderIcon, info.getOrderChildImg());
holder.orderName.setText(info.getOrderChildName());
holder.orderCount.setText(info.getOrderChildCount());
holder.orderPrice.setText(info.getOrderChildPrice());
return view;
}

class OrderChildHolder {
ImageView orderIcon = null;
TextView orderName = null;
TextView orderCount = null;
TextView orderPrice = null;
}

...全文
440 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
来根烟如何 2016-01-14
  • 打赏
  • 举报
回复
楼主,,你那个listView 嵌套listView显示错乱under问题解决了没有 我也遇到了
dasheng0306 2015-11-11
  • 打赏
  • 举报
回复
我的也是,就是子listview随着滚动会一直无限刷新。数据还会重复。请问楼主当时怎么解决的?
14梧桐雨 2015-09-20
  • 打赏
  • 举报
回复
楼主问题解决了吗,我也遇到了同样的问题,纠结好久了就是解决不了
boatwater 应用层 2015-08-11
  • 打赏
  • 举报
回复
我在想是不是 JSONObject jsonobject = new JSONObject(data); JSONArray array = jsonobject.getJSONArray("result"); for (int i = 0; i < array.length(); i++) { info = new Info(); JSONObject object = array.getJSONObject(i); String id = object.optString("id");// 订单号 String time = object.optString("createdate");// 时间 String score = object.optString("score");// 积分 String status = object.optString("statu");// 状态 String amount = object.optString("amount");// 合计 JSONArray jsonArray = object.getJSONArray("orderproduct"); for (int j = 0; j < jsonArray.length(); j++) { childInfo = new OrderChildInfo(); JSONObject objects = jsonArray.getJSONObject(j); String name = objects.optString("productname");// 商品 String number = objects.optString("productnumber");// 数量 String price = objects.optString("productprice");// 价格 childInfo.setOrderChildName(name); childInfo.setOrderChildCount(number); childInfo.setOrderChildPrice(price); childList.add(childInfo); } childAdapter = new OrderChildAdapter(context, childList); info.setOderCode("订单号" + id); info.setOderTime("时间:" + time); info.setOderIntegral("积分" + score); info.setOderPrice("合计" + amount); info.setOderStatus(status); info.setChildAdapter(childAdapter); list.add(info); } 这里面的问题,childList里面的数据一直在加
boatwater 应用层 2015-08-11
  • 打赏
  • 举报
回复

这个是截图
香甜可口的苹果是第二条的数据,宏碁是第三条,致逝去的青春是第四条,现在全都跑一块去了
这是第二条的截图,和第一条一样,下面的几条都是这种
boatwater 应用层 2015-08-11
  • 打赏
  • 举报
回复
引用 1 楼 come_on_Angelababy 的回复:
{ "code": "1", "result": [ { "id": "10259", "createdate": "2014-06-30 21:23", "amount": "95.00", "score": "0", "paytype": "1", "statu": "已取消", "iscomment": "null", "orderproduct": {//这个地方对应的是个对象 "picture": "/attached/image/20140120/1390185473547_3.jpg", "productid": "10308", "productname": "钢铁是怎样炼成的", "productnumber": "1", "productprice": "90.0" } }, { "id": "10228", "createdate": "2014-03-04 17:50", "amount": "1085.00", "score": "0", "paytype": "1", "statu": "已取消", "iscomment": "null", "orderproduct": [//这个对应的是个数组 { "picture": "/attached/image/20140304/1393898360387_3.jpg", "productid": "10262", "productname": "儿童 新生婴儿纯棉防水围脖 围嘴 口水巾 宝宝超可爱绣花围兜围嘴", "productnumber": "1", "productprice": "12.0" }, { "picture": "/attached/image/20140304/1393898360387_3.jpg", "productid": "10269", "productname": "玛尚N6000 USB笔记本电脑有线鼠标 女生可爱鼠标有线 USB鼠标", "productnumber": "1", "productprice": "16.0" } ] } ] }
哦,这个是少打了,应该是数组
come_on_Angelababy 2015-08-11
  • 打赏
  • 举报
回复
{ "code": "1", "result": [ { "id": "10259", "createdate": "2014-06-30 21:23", "amount": "95.00", "score": "0", "paytype": "1", "statu": "已取消", "iscomment": "null", "orderproduct": {//这个地方对应的是个对象 "picture": "/attached/image/20140120/1390185473547_3.jpg", "productid": "10308", "productname": "钢铁是怎样炼成的", "productnumber": "1", "productprice": "90.0" } }, { "id": "10228", "createdate": "2014-03-04 17:50", "amount": "1085.00", "score": "0", "paytype": "1", "statu": "已取消", "iscomment": "null", "orderproduct": [//这个对应的是个数组 { "picture": "/attached/image/20140304/1393898360387_3.jpg", "productid": "10262", "productname": "儿童 新生婴儿纯棉防水围脖 围嘴 口水巾 宝宝超可爱绣花围兜围嘴", "productnumber": "1", "productprice": "12.0" }, { "picture": "/attached/image/20140304/1393898360387_3.jpg", "productid": "10269", "productname": "玛尚N6000 USB笔记本电脑有线鼠标 女生可爱鼠标有线 USB鼠标", "productnumber": "1", "productprice": "16.0" } ] } ] }

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧