我有一个微调将显示所有来自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
很高兴为您效劳。 –