I am developing a android application with spinner in a form. The spinner items and spinner values are different. I want to collect all the value from the from including spinner and set a rest api service to web back end.
here is the response array.
{"Status":true,"errorType":null,"countryList":[{"Code":"US","Name":"United States"},{"Code":"CA","Name":"Canada"},{"Code":"AU","Name":"Australia"},{"Code":"GB","Name":"United Kingdom"}]}
I am successfully binded the Name jsonObject to spinner but I cant add the Code.
here is my code.
JSONObject responseObject = new JSONObject(res);
String status=responseObject.getString("Status");
JSONArray JA = responseObject.getJSONArray("countryList");
JSONObject json= null;
if(status.equals("true")) {
final String[] str2 = new String[JA.length()];
for (int i=0;i<JA.length();i++)
{
json = JA.getJSONObject(i);
str2[i] = json.getString("Name");
}
sp = (Spinner) findViewById(R.id.citnzshp_field);
list = new ArrayList<String>();
for(int i=0;i<str2.length;i++)
{
list.add(str2[i]);
}
Collections.sort(list);
ArrayAdapter<String> adp;
adp = new ArrayAdapter<String>
(getApplicationContext(),android.R.layout.simple_dropdown_item_1line, list);
sp.setAdapter(adp);
}
How can I bind the code jsonObject to spinner for taking after form submits.
Can anyone please share me the advantage of making custom adapter for binding data to spinner and selecting value also.
@Haresh Chhelana example is good, However if you want to show both name and code in spinner after selecting, check this out.
And Spinner selected item callback