我有一個微調將顯示所有來自JSON的供應商名稱。我已經檢索了JSON數據,將它存儲在一個數組列表中並顯示出來。問題是它只顯示一個項目。最近添加的項目。微調只顯示JSON中的一個項目
這裏是我的代碼包含了微調的實現:
@Override
protected void onPostExecute(JSONObject json){
if(json != null){
try{
result = json.getJSONArray("supplier");
if(!result.toString().equals("[]")) {
for (int i = 0; i < result.length(); i++) {
JSONObject source = result.getJSONObject(i);
String suppliers = source.getString("SupplierName");
//Toast.makeText(PurchaseOrder.this, "Suppliers: "+suppliers, Toast.LENGTH_SHORT).show();
spinnerArray = new ArrayList<String>();
spinnerArray.add(suppliers);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
PurchaseOrder.this, android.R.layout.simple_spinner_item, spinnerArray);
spinSupplier.setAdapter(adapter);
}
} else {
Toast.makeText(PurchaseOrder.this, "ERROR", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.fillInStackTrace();
}
} else if(json == null) {
Toast.makeText(PurchaseOrder.this, "NULL", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(PurchaseOrder.this, "ERROR", Toast.LENGTH_SHORT).show();
}
}
JSON:將物品放入數組後
{"supplier":[{"SupplierID":"1","SupplierName":"Nike","Address":"161","City":"Caloocan","Region":"NCR","PostalCode":"1114","Phone":"0917123456","Email":"[email protected]"},{"SupplierID":"2","SupplierName":"Adidas","Address":"36 C","City":"Quezon City","Region":"NCR","PostalCode":"1115","Phone":"7493857","Email":"[email protected]"},{"SupplierID":"3","SupplierName":"NIEK","Address":"","City":"Quezon City","Region":"ARMM","PostalCode":"1104","Phone":"709-2227","Email":"[email protected]"}]}
我做了你的代碼。以及交換列表中的聲明和添加項目,並且工作。在我的代碼中忽略了它。謝謝。 – JJCADIZ
很高興爲您效勞。 –