2017-11-18 175 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並傳遞給你的適配器。