如何获得JSON对象按微调选择 [英] How to get json object as per spinner selection
本文介绍了如何获得JSON对象按微调选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从这种反应越来越姓名和日期在我的微调,现在我所想的,如果用户从微调百合选择,然后我想在字符串OCC价值,但我没有得到它,下面是我的反应和code,任何一个可以告诉我,什么是问题
[
{
friend_id:1,
user_spoc_dob:1993年11月11日,
状态:真正的,
spouse_name:百合,
OCC:SPOC
},
{
friend_id:1,
user_mother_dob:1974年12月12日,
状态:真正的,
mother_name:SITA,
消息:详细地址不可用,
OCC:妈妈
},
{
friend_id:1,
user_father_dob:1972年11月19日,
状态:真正的,
father_name:公羊,
消息:详细地址不可用,
OCC:爸爸
},
{
friend_id:1,
user_dob:1994年2月20日,
状态:真正的,
user_fname:苏,
OCC:杀破狼
}
]
MainActivity.java
保护的ArrayList<串GT; doInBackground(
字符串参数... args){
SH的ServiceHandler =新的ServiceHandler(); statedata =新的ArrayList<串GT;();
字符串jsonStr = sh.makeServiceCall(STATE_URL,ServiceHandler.GET);
Log.d(回应:,>中+ jsonStr);
如果(jsonStr!= NULL){
尝试{
jsonObj =新JSONArray(jsonStr);
的for(int i = 0; I< jsonObj.length();我++){
JSONObject的C = jsonObj.getJSONObject(I)
串wifebday =(c.has(user_spoc_dob))? c.getString(user_spoc_dob):空;
串wifename =(c.has(spouse_name))? c.getString(spouse_name):空;
串mothersbday =(c.has(user_mother_dob))? c.getString(user_mother_dob):空;
串mothersname =(c.has(mother_name))? c.getString(mother_name):空;
串fatherbday =(c.has(user_father_dob))? c.getString(user_father_dob):空;
串fathername =(c.has(father_name))? c.getString(father_name):空;
串ownbbday =(c.has(user_dob))? c.getString(user_dob):空;
串ownname =(c.has(user_fname))? c.getString(user_fname):空;
OCCS =(c.has(OCC))? c.getString(OCC):空;
的System.out.println(BDAY+ wifebday + mothersbday + fatherbday + ownbbday);
// if测试,以避免增加空值
如果(wifebday!= NULL)statedata.add(wifename +:+ wifebday);
如果(mothersbday!= NULL)statedata.add(mothersname +:+ mothersbday);
如果(fatherbday!= NULL)statedata.add(fathername +:+ fatherbday);
如果(ownbbday!= NULL)statedata.add(ownname +:+ ownbbday);
testlist =新的ArrayList<串GT;();
如果(!OCCS = NULL)testlist.add(OCCS); } }赶上(JSONException E){
e.printStackTrace();
}
}其他{
Log.e(的ServiceHandler,无法从URL得到任何数据);
}
返回statedata;
} 保护无效onPostExecute(ArrayList的<串GT;的结果){
super.onPostExecute(结果);
pDialog.dismiss();
arrallstates =新的String [statedata.size()]; //你也可以使用结果,而不是数组
对于(INT指数= 0;指数 - LT; statedata.size();指数++){
arrallstates [指数] = statedata.get(指数); //或result.get(指数);
// arrallstates [指数] = testlist.get(指数);
}
//传递arrConuntry数组ArrayAdapter<串GT; constroctor:
adapterallstates =新ArrayAdapter<串GT;(
WishFriendsFamily.this.getActivity(),
android.R.layout.simple_spinner_dropdown_item,arrallstates);
的System.out.println(adpttest+ adapterallstates);
staticSpinner.setAdapter(adapterallstates); staticSpinner.setOnItemSelectedListener(新AdapterView.OnItemSelectedListener(){
@覆盖
公共无效onItemSelected(适配器视图<>适配器视图,视图观点,INT I,长L){ 串光正交码= testlist.get(ⅰ)的ToString();
的System.out.println(slected触网+光正交码);
}
@覆盖
公共无效onNothingSelected(适配器视图<>适配器视图){ }
});
}
}
解决方案
下面:
testlist =新的ArrayList<串GT;();
如果(OCCS!= NULL)
testlist.add(OCCS);
这些线路引起的问题,因为 testlist
正在初始化每次。移动 testlist
初始化出端 for循环
:
jsonObj =新JSONArray(jsonStr);
testlist =新的ArrayList<串GT;();
的for(int i = 0; I< jsonObj.length();我++){ // ...您code这里JSON解析
如果(OCCS!= NULL)
testlist.add(OCCS);
其他
testlist.add();
}
I am getting name and date in my spinner from this response,now what i am trying is if user select lily from spinner then i want to get occ value in string,but i am not getting it,following is my response and code,can any one tell me that what is the issue
[
{
"friend_id": "1",
"user_spoc_dob": "1993-11-11",
"status": "true",
"spouse_name": "lily",
"occ": "spoc"
},
{
"friend_id": "1",
"user_mother_dob": "1974-12-12",
"status": "true",
"mother_name": "sita",
"message": "Address details Not available",
"occ": "mom"
},
{
"friend_id": "1",
"user_father_dob": "1972-11-19",
"status": "true",
"father_name": "ram",
"message": "Address details Not available",
"occ": "dad"
},
{
"friend_id": "1",
"user_dob": "1994-02-20",
"status": "true",
"user_fname": "Su",
"occ": "spl"
}
]
MainActivity.java
protected ArrayList<String> doInBackground(
String... args) {
ServiceHandler sh = new ServiceHandler();
statedata = new ArrayList<String>();
String jsonStr = sh.makeServiceCall(STATE_URL, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
jsonObj = new JSONArray(jsonStr);
for (int i = 0; i < jsonObj.length(); i++) {
JSONObject c = jsonObj.getJSONObject(i);
String wifebday= (c.has("user_spoc_dob")) ? c.getString("user_spoc_dob") : null;
String wifename= (c.has("spouse_name")) ? c.getString("spouse_name") : null;
String mothersbday= (c.has("user_mother_dob")) ? c.getString("user_mother_dob") : null;
String mothersname= (c.has("mother_name")) ? c.getString("mother_name") : null;
String fatherbday= (c.has("user_father_dob")) ? c.getString("user_father_dob") : null;
String fathername= (c.has("father_name")) ? c.getString("father_name") : null;
String ownbbday= (c.has("user_dob")) ? c.getString("user_dob") : null;
String ownname= (c.has("user_fname")) ? c.getString("user_fname") : null;
occs= (c.has("occ")) ? c.getString("occ") : null;
System.out.println("Bday" + wifebday + mothersbday + fatherbday + ownbbday);
// if test, to avoid adding null values
if(wifebday!=null) statedata.add(wifename+" : "+wifebday);
if(mothersbday!=null) statedata.add(mothersname+" : "+mothersbday);
if(fatherbday!=null) statedata.add(fathername+" : "+fatherbday);
if(ownbbday!=null) statedata.add(ownname+" : "+ownbbday);
testlist=new ArrayList<String>();
if(occs!=null) testlist.add(occs);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return statedata;
}
protected void onPostExecute(ArrayList<String> result) {
super.onPostExecute(result);
pDialog.dismiss();
arrallstates = new String[statedata.size()]; // you can also use "result" array instead
for (int index = 0; index < statedata.size(); index++) {
arrallstates[index] = statedata.get(index);// or result.get(index);
//arrallstates[index] =testlist.get(index);
}
// pass arrConuntry array to ArrayAdapter<String> constroctor :
adapterallstates = new ArrayAdapter<String>(
WishFriendsFamily.this.getActivity(),
android.R.layout.simple_spinner_dropdown_item, arrallstates);
System.out.println("adpttest" + adapterallstates);
staticSpinner.setAdapter(adapterallstates);
staticSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String oocs=testlist.get(i).toString();
System.out.println("slected ocs" + oocs);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
解决方案
Here:
testlist=new ArrayList<String>();
if(occs!=null)
testlist.add(occs);
these lines causing issue, because testlist
is initializing every time. move testlist
initialization out-side for-loop
:
jsonObj = new JSONArray(jsonStr);
testlist=new ArrayList<String>();
for (int i = 0; i < jsonObj.length(); i++) {
//... your code here for json parsing
if(occs!=null)
testlist.add(occs);
else
testlist.add("");
}
这篇关于如何获得JSON对象按微调选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文