I have string array which contains all the country names and another string array with respective country codes. I am setting the country names string to spinner. When I sel
String[] countries = {"INDIA","DUBAI","KSA"};
String[] country_code = {"91","971","966"};
final Spinner spinnerCountry = (Spinner)findViewById (R.id.spinnerCountry);
final Spinner spinnerCountryCode = (Spinner) findViewById (R.id.spinnerCountryCode);
ArrayAdapter<String> countryAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,countries);
ArrayAdapter<String> countryCodeAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,country_code);
spinnerCountry.setAdapter(countryAdapter);
spinnerCountryCode.setAdapter(countryCodeAdapter);
spinnerCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3) {
spinnerCountryCode.setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
spinnerCountryCode.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3) {
spinnerCountry.setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Here is some code that may fix your problem.
Explanation:
As you can see i created two arrays which contains countryName and countryCode respectively.
Work Flow:-
When i select one item in a Spinner then the other spinner loads with another item corresponding to the position.
The code is Self-Explanatory!
String businesstype_array[] = {"Retail", "Vendor","Agriculture" };
public static final String JSON_STRING = "{\"Retail\":[\"General Stores\",\"Clothing\",\"Electronics\",\"Mobile Phones\"],\"Vendor\":[\"Gas agency\",\"newspaper\",\"vendor\",\"milk vendor\",\"laundry\"],\"Agriculture\":[\"lGas agency\",\"lnewspaper\",\"lvendor\",\"lmilk vendor\",\"llaundry\"]}";
ArrayAdapter adapter_business = new ArrayAdapter(getActivity(), R.layout.singlerow_businesstype_spinner, businesstype_array); businesstype.setAdapter(adapter_business);
businesstype.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Log.d("jsonntring", "out tryy");
try {
Log.d("jsonntring", "in tryy");
jsonObject = new JSONObject(JSON_STRING);
Log.d("jsonntring", jsonObject.toString() + "length==" + jsonObject.length());
JSONArray jsonarray = jsonObject.getJSONArray(businesstype.getSelectedItem().toString());
Log.d("jsonarray&", jsonarray.toString());
businesstype__subtype.clear();
for (int j = 0; j < jsonarray.length(); j++) {
String s = jsonarray.getString(j);
businesstype__subtype.add(s);
Log.d("jsonarray&", jsonarray.toString());
}
ArrayAdapter adapter_sub_business = new ArrayAdapter(getActivity(), R.layout.singlerow_businesstype_spinner, businesstype__subtype);
business_subtype.setAdapter(adapter_sub_business);
} catch (JSONException e)
{
Log.d("jsonntring_exce", e.toString());
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});