0

I'm doing an application that need to receive an unknow number of names with they and a id.

I have solved the php and mysql part and this is what I receive into my app:

[{"id":"3","nombre":"Kaotik Sabadel"},{"id":"4","nombre":"Promotor Kaotik"}]

In the android part I start with this:

myJsonObj = new JSONObject(jodata);

and that's all I have.

And I want to set the names (nombre) into an spinner and when the user choose this name, get the value of his id...

rekire
  • 47,260
  • 30
  • 167
  • 264

1 Answers1

0

You need to implement a custom SpinnerAdapter which parses the JSONArray (and no JSONObject) and fills the data into your Spinner.

As a little hint how to parse the data use this code:

JSONArray data=new JSONArray(json);
for(int i=0; i<data.length(); i++) {
    JSONObject element=data.getJSONObject(i);
    int id=element.getInt("id");
    // ...
}

The part with the id may failes because it is a string and no integer just now I don't know if JSONObject can detect that the value can be casted to an int. If not use getString and parse it to a int.

rekire
  • 47,260
  • 30
  • 167
  • 264