遍历JSONObject并动态填充微调框

时间:2019-10-27 16:16:56

标签: android json android-volley

我正在android上使用volley库,我必须解析此JSON,并用一些来自volley响应的值填充我的微调器,这些值嵌套在另一个json对象中。

这里是JSON:

{
"Count": 2,
"0": {
    "Distributore Information": {
        "id_distributore": "1",
        "NomeDistributore": "Colonnina 1",
    }
},
"1": {
    "Distributore Information": {
        "id_distributore": "2",
        "NomeDistributore": "Colonnina 2",
    }
}
}

这是我写的代码:

private void getInfoColonnina(){
    String url = "https://api.thisurlsidhiddenbutitsworknp_module_json";
    final SharedPreferences myPref = getSharedPreferences("loginPref", MODE_PRIVATE);
    final SharedPreferences.Editor myPreff = myPref.edit();

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Toast.makeText(InizioTurnoActivity.this, "CHIAMATA INFOCOLONNINA AVVENUTA CON SUCCESSO", Toast.LENGTH_SHORT).show();
            try {
                JSONObject jsonObject = new JSONObject(response);
                Iterator<String> keys = jsonObject.keys();

                while(keys.hasNext()) {
                    String key = keys.next();
                    if (jsonObject.get(key) instanceof JSONObject) {

                        JSONObject infoDistributore = jsonObject.getJSONObject("Distributore Information");
                        LitriColonnina = infoDistributore.getString("litriiniziocolonna");
                        id_distributore = infoDistributore.getString("id_distributore");
                        NomeDistributore = infoDistributore.getString("NomeDistributore");
                    }
                }

                    final DistrBean distrTest = new DistrBean(NomeDistributore, id_distributore, LitriColonnina);

                    MaterialSpinner spinner = (MaterialSpinner) findViewById(R.id.spinner);
                    spinner.setItems(distrTest.getNomeDistributore());
                    spinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
                        @Override public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
                            Toast.makeText(InizioTurnoActivity.this, distrTest.getIdDistributore(), Toast.LENGTH_SHORT).show();
                            myPreff.putString("id_distributore", distrTest.getIdDistributore());
                            myPreff.commit();
                        }
                    });

            } catch (JSONException e) {
                e.printStackTrace();
            }

因此,代码应进行迭代,并创建可填充微调器但不起作用的对象。如果我仅解析一个对象并填充微调框,它可以正常工作并显示出来,但是我真的无法弄清楚如何通过JSON循环获取我需要的所有数据,每个“ id_distributore”和“ NomeDistributore”有什么想法? / p>

提前谢谢!

1 个答案:

答案 0 :(得分:0)

您应将ArrayAdapter用作spinner,而不是使用ArrayAdapter来将元素添加到List

因此,将您的元素动态添加到List中,然后在for loop中将list放入ArrayAdapter中之后,最后将adapter设置为{{1 }}

示例:

spinner
  

Helpfull:

此外,您还可以在此之后向微调器中添加其他元素,方法是将这些元素添加到列表中,而不是使用不使用的适配器:

ArrayAdapter<String> arrayAdapter;
arrayAdapter= new ArrayAdapter<String>(this, R.layout.spinner_indication, yourList); //yourList is the List your create dynamically in the for loop with the elements from the Json file
Spinner yourSpinner;
yourSpinner.setAdapter(arrayAdapter);
  

编辑:

您的Json文件格式错误,它应如下所示:

arrayAdapter.notifyDataSetChanged();

在您的php文件中使用:

{
    "Count": "2",
  "0": {
    "Distributore Information": {
      "id_distributore": "1",
      "NomeDistributore": "Colonnina 1"
    }
  },
  "1": {
    "Distributore Information": {
      "id_distributore": "2",
      "NomeDistributore": "Colonnina 2"
    }
  }
}

检查您的Json here