Add a first value to spinner when spinner data is came from server
Add String -Select Your Choice- as the first element in the array List.
String choice1 = "-Select Your Choice-";
allNames.add(choice1);
JSONArray array = jsonObject.getJSONArray("Spinner value");
for (int i = 0; i < array.length(); i++) {
JSONObject job = array.getJSONObject(i);
String name = job.getString("spinner_data");
allNames.add(name);
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
(MainActivity.this, android.R.layout.simple_spinner_item, allNames);
spinner.setAdapter(dataAdapter);
Try this
JSONArray array = jsonObject.getJSONArray("Spinner value");
allNames.add("Select Your Choice");
for (int i = 0; i < array.length(); i++) {
JSONObject job = array.getJSONObject(i);
String name = job.getString("spinner_data");
allNames.add(name);
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
(MainActivity.this, android.R.layout.simple_spinner_item, allNames);
spinner.setAdapter(dataAdapter);
allNames.clear();
allNames.add("Select Vehicle");
JSONArray array = jsonObject.getJSONArray("Spinner value");
for (int i = 0; i < array.length(); i++)
{
JSONObject job = array.getJSONObject(i);
String name = job.getString("spinner_data");
allNames.add(name);
}
Do this,
allNames.add("select your choice")
JSONArray array = jsonObject.getJSONArray("Spinner value");
for (int i = 0; i < array.length(); i++) {
JSONObject job = array.getJSONObject(i);
String name = job.getString("spinner_data");
allNames.add(name);}
It will automatically set first index as choice option.