1

I want to implement one spinner where items comes from server and I want to add "Select item" on first row of spinner

This is spinner

    <Spinner
          android:background="@drawable/spinner_back"
          android:id="@+id/eduMinPart"
          android:layout_width="350dp"
          android:layout_height="50dp"
          android:textSize="18dp"
          style="@style/Base.Widget.AppCompat.Spinner.Underlined"
          android:layout_below="@+id/EducationTV"
          android:layout_centerHorizontal="true"
          android:layout_marginTop="2dp"/> 

This is java code

  StringRequest strReqests = new StringRequest(Request.Method.GET,EDU_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    String[] arr = response.trim().split(",");
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>
          (EditDetails7.this, android.R.layout.simple_list_item_1, arr);
           adapter.setDropDownViewResource
                  (android.R.layout.simple_spinner_dropdown_item);
           spinEducation.setAdapter(adapter);
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(EditDetails7.this, error.toString(), 
             Toast.LENGTH_LONG).show();
        }
    });
    AppController.getInstance().addToRequestQueue(strReqests);;

I used adapter.add("select Education") and prompt but both is not working.

Vikas Godiyal
  • 117
  • 3
  • 15

3 Answers3

3

This will work for you.

int arraySize = response.trim().split(",").length + 1;
String[] arr = new String[arraySize];
arr[0] = "select Item";
arr = response.trim().split(",");
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Pallavi Tapkir
  • 781
  • 7
  • 28
  • I used this one String s2 = "Select Item," + response.trim(); arr=s2.trim().split(","); In your code arr 0th position takes item 'select Item' but when you override it arr=response.trim().split(","); arr 0th takes first item of response. – Vikas Godiyal Aug 09 '17 at 11:12
  • Why don't you try ArrayList instead of Array?? – Pallavi Tapkir Aug 09 '17 at 11:17
1
String temp="select your what ever,"+response;
temp=response.trim().split(",");
Sai Jayant
  • 366
  • 2
  • 14
  • I used this one String s2 = "Select Item," + response.trim(); arr=s2.trim().split(","); In your code arr 0th position takes item 'select Item' but when you override it arr=response.trim().split(","); arr 0th takes first item of response. – Vikas Godiyal Aug 09 '17 at 11:16
  • you can refer to https://stackoverflow.com/questions/14518195/how-can-i-add-new-item-to-the-string-array – Sai Jayant Aug 09 '17 at 11:19
  • Please put a little bit more explanation as to how the code above solves the issue – Adonis Aug 09 '17 at 11:25
  • @VikasGodiyal please try above code let me know if that works. – Sai Jayant Aug 09 '17 at 11:35
0

Hey why don't you use the ArrayList<String> in Adapter instead of Array.

Example:

ArrayList<String> arr = new ArrayList<String>(Arrays.asList(response.trim().split(",")));
arr.add(0, "Select Item")
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(EditDetails7.this, android.R.layout.simple_list_item_1, arr);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinEducation.setAdapter(adapter);

Note:

The "Select Item" will be selectable from the Spinner. If you need to put it just as a header of the Spinner then you need to find some other solution.

iMDroid
  • 2,108
  • 1
  • 16
  • 29
  • If I use this code and spinner item is 'Select Item' then I will click on submit button so I need blank will go to server. – Vikas Godiyal Aug 09 '17 at 11:44
  • This issue is remain – Vikas Godiyal Aug 09 '17 at 11:50
  • Okay.. This might help you https://stackoverflow.com/questions/867518/how-to-make-an-android-spinner-with-initial-text-select-one/12221309#12221309 – iMDroid Aug 09 '17 at 11:53
  • Before this implementation I used spinner.setPrompt("Select your favorite Planet!"); but it was not working on my spinner – Vikas Godiyal Aug 09 '17 at 11:57
  • I have one more idea to avoid this issue but I don't know my idea is good or not but it's working for me. String s6 = (spinner.equalsIgnoreCase("Select Item") ? "" : spinner); I used this code when I send data to server – Vikas Godiyal Aug 10 '17 at 09:46