我有一个微调器,它将显示来自JSON的所有供应商名称。我检索了JSON数据,将其存储在arraylist中并显示它。问题是它只显示一个项目。最近添加的项目。
这是我的代码,其中包含微调器的实现:
@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@mail.com"},{"SupplierID":"2","SupplierName":"Adidas","Address":"36 C","City":"Quezon City","Region":"NCR","PostalCode":"1115","Phone":"7493857","Email":"email@email.com"},{"SupplierID":"3","SupplierName":"NIEK","Address":"","City":"Quezon City","Region":"ARMM","PostalCode":"1104","Phone":"709-2227","Email":"niekniekniek@gmail.com"}]}
答案 0 :(得分:3)
只需在将项目添加到数组后设置适配器,如下所示:
@Override
protected void onPostExecute(JSONObject json){
if(json != null){
try{
result = json.getJSONArray("supplier");
if(!result.toString().equals("[]")) {
spinnerArray = new ArrayList<String>();
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.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();
}
}
答案 1 :(得分:1)
有两个问题:
spinnerArray = new ArrayList<String>();
它应该是外部&amp;之前 for
循环。
spinSupplier.setAdapter(adapter);
它应该是外部&amp;在 for
循环之后。
解决方案:
spinnerArray = new ArrayList<String>();
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.add(suppliers);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
PurchaseOrder.this, android.R.layout.simple_spinner_item, spinnerArray);
spinSupplier.setAdapter(adapter);
答案 2 :(得分:0)
试试这个循环:
在for循环后设置适配器 在for循环之前创建数组对象
spinnerArray = new ArrayList<String>();
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.add(suppliers);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
PurchaseOrder.this, android.R.layout.simple_spinner_item, spinnerArray);
spinSupplier.setAdapter(adapter);
答案 3 :(得分:0)
您需要删除setAdapter代码以及ArrayList
的初始化 @Override
protected void onPostExecute(JSONObject json){
if(json != null){
try{
result = json.getJSONArray("supplier");
if(!result.toString().equals("[]")) {
spinnerArray = new ArrayList<String>();
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.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();
}
}
答案 4 :(得分:0)
我相信您一次又一次初始化ArrayList
,检查您的代码和
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.add(suppliers);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
PurchaseOrder.this, android.R.layout.simple_spinner_item, spinnerArray);
spinSupplier.setAdapter(adapter);
}
移到线下
spinnerArray = new ArrayList<String>();
在此行之前
if(!result.toString().equals("[]"))
它应该有用。
答案 5 :(得分:0)
在for循环
之前声明spinnerArray
spinnerArray = new ArrayList<String>();
在for循环后将适配器放在外面
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
PurchaseOrder.this, android.R.layout.simple_spinner_item, spinnerArray);
spinSupplier.setAdapter(adapter);