Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a first value to spinner when spinner data is came from server

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);
                
like image 221
Avinash Avatar asked Dec 26 '17 06:12

Avinash


2 Answers

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);
like image 200
Tomin B Azhakathu Avatar answered Sep 20 '22 20:09

Tomin B Azhakathu


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);
like image 25
Ratilal Chopda Avatar answered Sep 18 '22 20:09

Ratilal Chopda