【发布时间】:2016-10-29 15:53:24
【问题描述】:
我有这个 json :
"result": [
{
"nama_p": "ACEH",
"IDProvinsi": "1"
},
{
"nama_p": "SUMATERA UTARA",
"IDProvinsi": "6728"
},
{
"nama_p": "SUMATERA BARAT",
"IDProvinsi": "12920"
}]
当我在微调器上显示 nama_p 时,我一直在尝试获取 IDProvinsi 值。 但是我失败了..
这是我的 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. 展示城市
我想在状态值发送到我的网络服务时显示城市
感谢堆栈溢出
【问题讨论】:
-
似乎json解析没有问题只需在
getProv(result);函数调用之后设置你的适配器 -
在我显示 nama_p 之前没有问题 .. 但是当微调器选择先生时如何获取 IDProvinsi ?
-
在 spinner 上使用监听器,将为您提供位置,然后使用位置从您的
id列表中获取数据 -
你能告诉我或给我代码或链接吗?或者请提出这个问题:)
-
这是另一回事,你需要做的是,首先从第一个微调器中获取选定的项目,然后用适当的城市数组初始化第二个微调器,或者你可以更改第二个微调器列表的数据,试试这个链接@ 987654321@