Spinner只显示JSON中的一个项目

时间:2017-05-05 04:39:54

标签: android json spinner

我有一个微调器,它将显示来自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"}]}

6 个答案:

答案 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)

有两个问题:

  1. 初始化数组spinnerArray = new ArrayList<String>();
  2. 的行

    它应该是外部&amp;之前 for循环。

    1. 您已设置适配器spinSupplier.setAdapter(adapter);
    2. 的行

      它应该是外部&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);