0

I am using Json Asp.NET Webservice and Android Soap service to retrive data.
And It is working fine. Now, I want to set Value and Text in Spinner from ArrayAdapter & Gson. How to do it ?

My Code :

placelist = gson.fromJson(result, City[].class);
ArrayAdapter<City> adapter = new ArrayAdapter<City>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, placelist);
spinnerFood.setAdapter(adapter);


My Output:

[{"CityId":1,"CityName":"Vadodara"},{"CityId":2,"CityName":"ahmedabad"},{"CityId":3,"CityName":"Gandhinagar"},{"CityId":4,"CityName":"Bhavnagar"},{"CityId":7,"CityName":"Eluru"},{"CityId":8,"CityName":"Visakhapatnam"},{"CityId":15,"CityName":"Anantapur"},{"CityId":16,"CityName":"Srikakulam"}]

I want to set CityName as Spinner Text and CityId as Spinnet Value. City is java class file which contains CityId and CityName parameters.

Jeeten Parmar
  • 5,568
  • 15
  • 62
  • 111

2 Answers2

1

If you want only CityName to be dropdown value of Spinner

then you should pass only the list of CityName not the array of City class to the adapter. So, to extract the CityName from your array you can do like this:

List lList = Arrays.asList(placelist);
List<String> cityName = new ArrayList<String>();
List<String> cityId = new ArrayList<String>();
City obj;

for (int i=0; i < lList .size(); i++)
{
obj= lList.get(i);
cityName.add(obj.getCityName());
cityId.add(obj.getCityId()); // City Id will be the value for on click of spinner items

}

Now you can pass this cityName list to you arrayadapter:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, cityName);

Coming to Second part of your question, you need to set a listener for spinner where you can get Value of respective cityId's from "cityId" array list created

   public class CustomOnItemSelectedListener implements OnItemSelectedListener {
             String Id;
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
            // TODO Auto-generated method stub


            id= cityId.get(pos);
        }

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

        }

    } 

Let me know if it works

Regards

Amritesh
  • 96
  • 7
  • I want CityId as value and CityName as Text. – Jeeten Parmar Jan 27 '14 at 09:40
  • So, Please clarify... what do you want in spinner drop-down list and what should happen on click of that spinner item? – Amritesh Jan 27 '14 at 09:46
  • Just forget about on click of that spinner. I was CityId as Value and CityName as Text in Spinner. – Jeeten Parmar Jan 27 '14 at 10:03
  • I am not still clear with your questions but it seems u want both city id and city name in drop-down list value of spinner.If this is the case you need to have a custom adapter which will accept array/array list of your "City" class. Please look into this [post](http://stackoverflow.com/questions/1625249/android-how-to-bind-spinner-to-custom-object-list) for custom adapter for spinner.Let me know if you need help in this. – Amritesh Jan 27 '14 at 10:27
  • Sorry, I have tried your code and I got watever i wanted. Thank you... :) – Jeeten Parmar Jan 27 '14 at 10:56
0

Please follow the following tutorial Json parsing

First retrieve the response in JsonArray and From array retrieve JsonObjects. and store both in array, and you can use that value for spinner

Ameer
  • 2,709
  • 1
  • 28
  • 44