2016-10-29 40 views
1

我有此JSON:與不同的返回值顯示文本上微調的Android

"result": [ 
    { 
     "nama_p": "ACEH", 
     "IDProvinsi": "1" 
    }, 
    { 
     "nama_p": "SUMATERA UTARA", 
     "IDProvinsi": "6728" 
    }, 
    { 
     "nama_p": "SUMATERA BARAT", 
     "IDProvinsi": "12920" 
    }] 

我一直在試圖讓IDProvinsi值當我在我的微調顯示nama_p .. BU IM失敗..

這是我的Java代碼:

private void getData(){ 
     //Creating a string request 
     StringRequest stringRequest = new StringRequest(Config.DATA_URL, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         JSONObject j = null; 
         try { 
          //Parsing the fetched Json String to JSON Object 
          j = new JSONObject(response); 

          //Storing the Array of JSON String to our JSON Array 
          result = j.getJSONArray("result"); 

          //Calling method getStudents to get the students from the JSON Array 
          getProv(result); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 

        } 
       }); 

     //Creating a request queue 
     RequestQueue requestQueue = Volley.newRequestQueue(this); 

     //Adding request to the queue 
     requestQueue.add(stringRequest); 
    } 

    private void getProv(JSONArray j){ 
     //Traversing through all the items in the json array 
     for(int i=0;i<j.length();i++){ 
      try { 
       //Getting json object 
       JSONObject json = j.getJSONObject(i); 

       //Adding the name of the student to array list 
       id.add(json.getString("IDProvinsi")); 
       provinsi.add(json.getString("nama_p")); 
       System.out.println(provinsi); 

//    MyClass[] obj ={ 
//      new MyClass(provinsi,id) 
//    }; 

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

     //Setting adapter to show the items in the spinner 

     spinnerprov.setAdapter(new ArrayAdapter<String>(AddLokasiActivity.this, android.R.layout.simple_spinner_dropdown_item,id)); 

    } 

我如何得到IDProvinsi值,並將其發送到我的其他微調 我有2個微調
1.顯示狀態 2.顯示城市

我想顯示的城市時狀態的值發送給我的web服務

THX計算器

+0

好像有在JSON解析沒有問題,只要將適配器此'getProv(結果)後;'函數請撥打 –

+0

這個問題沒有問題,直到我顯示nama_p ..但如何獲得IDProvinsi時,微調選擇先生? –

+0

在微調器上使用監聽器,將給你的位置,然後使用位置從你的'ID'列表中獲取數據 –

回答

相關問題