You need to use the Activity context this
is not the activity context try like this
adapter= new ArrayAdapter<String>(Mainactivity.this, android.R.layout.simple_spinner_item, array);
Edit :
Change your array format. For example in your spinner you are going to show the subject so do the following
String[] mySubject = new String[array.length()];
for (int i = 0; i < array.length(); i++) {
JSONObject person = (JSONObject) array
.get(i);
System.out.println(person.toString());
String syllabus = person.getString("grade");
String grade = person.getString("grade");
String subject = person.getString("subject");
jsonResponse += "Board: " + syllabus + "\n\n";
jsonResponse2 += "Class: " + grade + "\n\n";
jsonResponse3 += "Subject: " + subject + "\n\n";
// jsonResponse4 += "ID: " + id + "\n\n";
mySubject[i] = subject;
}
adapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mySubject);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Note : Also you need to move your set adapter outside of for loop.
manpreet
Best Answer
2 years ago
I am getting my json in text box I tried to put those data in spinner but unable to do so. Below is my main activity class and I am using Volley.