-1

i have strange situation i am not getting idea how to bind this json response to spinner. i implement this way but not getting success all value.please suggest or help me to bind it .

json response

{
    "data": [
        {
            "gujarati": "Gujarati",
            "english": "English",
            "hindi": "Hindi",
            "hebrew": "Hebrew",
            "french": "French",
            "spanish": "Spanish",
            "arabic": "Arabic",
            "polish": "Polish",
            "bulgarian": "Bulgarian",
            "dude": "Dude",
            "all of them": "All Of Them",
            "uk english": "Uk English"
        }
    ]
}

add to list by key.

try {
                    root = new JSONObject(resplanguage);
                    JSONArray contacts = root.getJSONArray("data");
                    for (int i = 0; i < contacts.length(); i++) {

                        JSONObject c = contacts.getJSONObject(i);

                        languagelist.add(new LanguageData(c.getString("gujarati"), c.getString("english"), c.getString("hindi"),
                                c.getString("hebrew"), c.getString("french"), c.getString("spanish"), c.getString("arabic"),
                                c.getString("polish"), c.getString("bulgarian"), c.getString("dude"), c.getString("all of them"),
                                c.getString("uk english")));

                    }

                } catch (Exception e) {

                    e.printStackTrace();
                }

bind to spinner.

for (int k=0;k<languagelist.size();k++){
                    englisins[k]= String.valueOf(languagelist.get(k));
                }
                englishadapter= new ArrayAdapter<String>(ExploreInstructorActivity.this,
                        android.R.layout.simple_spinner_item, englisins);
                englishadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spEnglishSpeaking.setAdapter(englishadapter);
Hasu Sarkar
  • 59
  • 3
  • 13

1 Answers1

0

You are again not accessing the JSON object inside the array. Try this and let me know:

    try {
                        root = new JSONObject(resplanguage);
                        JSONArray contacts = root.getJSONArray("data");
                        JSONObject obj = contacts.getJSONObject(0);
                        Iterator it = obj.keys();
                        while(it.hasNext() ) {                       
                            languagelist.add((String)obj.get(it.next()));
                        }

                    } catch (Exception e) {

                        e.printStackTrace();
                    }
Aakash Verma
  • 3,705
  • 5
  • 29
  • 66