2017-11-18 64 views
2

我试图从MySQL数据库ArrayList对象,但结果添加对象上的ListView returing一行让我只 我使用自定义适配器一行对我ListView,我想我可以使用环多对象ArrayList的对象,但我失败了,请帮帮我,ArrayList对象是Java的Android

我的代码:

String driver_fullname = json.getString("driver_fullname"); 
String driver_phonenumber = json.getString("driver_phonenumber"); 
String plate_no = json.getString("plate_no"); 
String parking_name = json.getString("parking_name"); 

List<PaymentTiming_Items> getAllDiverDetails = new ArrayList<PaymentTiming_Items>(); 
PaymentTiming_Items timingItems = new PaymentTiming_Items(); 
timingItems.setPlateNo(plate_no); 
timingItems.setParkingName(parking_name); 
timingItems.setDriverFullName(driver_fullname); 
getAllDiverDetails.add(timingItems); // store all drivers' info to 
} 

if (getAllDiverDetails.size() !=0) { 
userList = new ArrayList<> (getAllDiverDetails); 
listAdapter = new PaymentTiming_ListAdapter(getApplicationContext(), userList); 
myList.setAdapter(listAdapter); 
} 
+0

你可以添加自定义布局xml ..检查其高度是否“匹配prent”。如果它match_parent改为wrap_content –

+0

@Tomin B自定义布局是在自定义适配器,我认为不是布局 –

+0

添加如何加载多个数据。代码中没有循环。 –

回答

2

看起来你正在创建一个ArrayList每次你解析的对象。如果我理解正确的话,你的代码应该是类似的东西:

// ArrayList will be created only once for a json response. 
    List<PaymentTiming_Items> getAllDiverDetails = new ArrayList<PaymentTiming_Items>(); 

    //Now parse add all elements in json response and add to list. 
     for(all items in your jsonResponse List) {  
      //Parse fields from json object 
      String driver_fullname = json.getString("driver_fullname"); 
      String driver_phonenumber = json.getString("driver_phonenumber"); 
      String plate_no = json.getString("plate_no"); 
      String parking_name = json.getString("parking_name"); 

      //create object 
      PaymentTiming_Items timingItems = new PaymentTiming_Items(); 
      timingItems.setPlateNo(plate_no); 
      timingItems.setParkingName(parking_name); 
      timingItems.setDriverFullName(driver_fullname); 
      getAllDiverDetails.add(timingItems); // store all drivers' info to 
     } 

     //Now list will have all the items, Add this list to adapter. 
      if (getAllDiverDetails.size() !=0) { 
      userList = new ArrayList<>(getAllDiverDetails); 
      listAdapter = new PaymentTiming_ListAdapter(getApplicationContext(), userList); 
      myList.setAdapter(listAdapter); 
      } 
+0

谢谢,你说的对,我每次都在解析一个对象 –

1

假设您的服务器给出的结果,在JSONArray说作为一个String 响应请尝试以下

List<PaymentTiming_Items> getAllDiverDetails = new ArrayList<PaymentTiming_Items>(); 


     JSONArray jsonArray = new JSONArray(response); 
     int size = jsonArray.length(); 
     if (size > 0) 
     { 
      for (int i = 0; i < size; i++) 
      { 
       JSONObject jsonObject = jsonArray.getJSONObject(i); 
       String driver_fullname = json.getString("driver_fullname"); 
       String driver_phonenumber = json.getString("driver_phonenumber"); 
       String plate_no = json.getString("plate_no"); 
       String parking_name = json.getString("parking_name"); 


       PaymentTiming_Items timingItems = new PaymentTiming_Items(); 
       timingItems.setPlateNo(plate_no); 
       timingItems.setParkingName(parking_name); 
       timingItems.setDriverFullName(driver_fullname); 
       getAllDiverDetails.add(timingItems); // store all drivers' info to 
      } 
     } 

if (getAllDiverDetails.size() !=0) { 
userList = new ArrayList<> (getAllDiverDetails); 
listAdapter = new PaymentTiming_ListAdapter(getApplicationContext(), userList); 
myList.setAdapter(listAdapter); 
} 
1

必须使用JSONArray为从JSON获取物品清单。然后用它们填充你的ArrayList并传递给你的适配器。