I want to set first spinner value as "select your choice" then shows the data from server one solution
creates another array list and in that list contain the value "select your choice" and combine these two and set to the spinner .. how can I achieve this please help me... (using cursor ????)
Spinner spinner;
private JSONArray result;
ArrayList<String> allNames = new ArrayList<String>();
spinner.setPrompt("...Select the Vehicle Number...");
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String vno = spinner.getSelectedItem().toString();
}
method for getting spinner data
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With