R Boughan R Boughan - 1 month ago 5
JSON Question

json parsing issues with spinners

I am getting some data by using php api and i am using JSON PARSING TO GET OUTPUT: BELOW IS THE CODE

private void showJSON(String json){
ParseJSON pj = new ParseJSON(json);
pj.parseJSON();
state_ids=ParseJSON.ids;
states=ParseJSON.names;
//state_nid=ParseJSON.name_nid;

//Creating the ArrayAdapter instance having the state list
ArrayAdapter a3 = new ArrayAdapter(UpdateUserProfile.this,android.R.layout.simple_spinner_item,states);
a3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
state_spin.setAdapter(a3);

}


here is parseJson()

protected void parseJSON(){
JSONObject jsonObject=null;
try {
jsonObject = new JSONObject(json);
users = jsonObject.getJSONArray(JSON_ARRAY);

ids = new String[users.length()];
names = new String[users.length()];
name_nid = new String[users.length()];

for(int i=0;i<users.length();i++){
JSONObject jo = users.getJSONObject(i);
ids[i] = jo.getString(KEY_ID);
names[i] = jo.getString(KEY_NAME);
name_nid[i]=ids[i]+" "+names[i];
/*int d=1;
String e=String.valueOf(d);*/
}
} catch (JSONException e) {
e.printStackTrace();
}
}


But the challenging thing for me is that whenever user will select any of the states i want to get the corresponding 'PHP Id' of that state. So that i can send another request to server in order to get the regions of that state. i am displaying corresponding regions on another spinner.

//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {

selected_state= (String)state_spin.getItemAtPosition(position);
//state_id_only=selected_state.replaceAll("[^0-9]", "");
//Log.d("state id is", state_id_only);
//sendRequest1();
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}


But on the 'state' spinner i only want to show states text not the ids. So what is the possible solution for this
I hope someone will understand my point and help me... i am a fresher..Thank you

Answer

As suggested by @VivekMishra in comments. i used that link and customized my spinner. Then i hide the visibility of ID textView so that in output i can get values of ID textview and due to visibility set its text will not appear in spinner output.

Comments