2

whenever i create a spinner by default it shows first element in spinner. i want to show my own hint on that spinner.

note:- i am receiving data from web services.

Xml

        <Spinner
            android:id="@+id/spinnerAtlasContactSignup"
            android:layout_width="200dip"
            android:layout_height="46dp"
            android:layout_below="@+id/editCompanySignup"
            android:layout_marginBottom="60dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/slect_box1x"
            android:ems="10"
            android:padding="10dp"
            android:prompt="@string/atlas_contact" />

Code -

private void initializeSpinner(ArrayList<AtlasContact> atlastContacts) {

ArrayAdapter<AtlasContact> adapter = new ArrayAdapter<AtlasContact>(this,android.R.layout.simple_spinner_item, atlastContacts);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinnerAtlasContact.setAdapter(adapter); 

here AtlasContact is a class which accepts the data coming from web services.

Shadab Ansari
  • 7,022
  • 2
  • 27
  • 45
Avinash Kumar Pankaj
  • 1,700
  • 2
  • 18
  • 27

3 Answers3

1

get all data using for loop and in this for loop create new arraylist and when i=0 so not add array list data and set new array list in spinner

Mahesh Kavathiya
  • 573
  • 5
  • 23
1

You can try below one

    private void initializeSpinner(ArrayList<AtlasContact> atlastContacts) {

    ArrayList<AtlasContact> atlastContactsDuplicate ; 
    For (int i= 0 ; i < atlastContacts+1 ; i++){
     if (i==0){
           atlastContactsDuplicate.add("") ; //this is null object which can be 
                                             //adjust           weight space     
        }
    else
    atlastContactsDuplicate.add(atlastContacts.get(i)) ; 
    }
        ArrayAdapter<AtlasContact> adapter = new ArrayAdapter<AtlasContact>(this,android.R.layout.simple_spinner_item, atlastContactsDuplicate);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinnerAtlasContact.setAdapter(adapter); 
}
dharmendra
  • 7,835
  • 5
  • 38
  • 71
  • sorry man but m gettin two errors 1.The operator + is undefined for the argument type(s) ArrayList, int. 2.The method add(AtlasContact) in the type ArrayList is not applicable for the arguments (String). please help – Avinash Kumar Pankaj Jan 02 '13 at 05:52
  • i have made a comment at atlastContactsDuplicate.add("") ; , you can not pass it as string of-course , pass blank value of AtlasContact class – dharmendra Jan 02 '13 at 06:25
0

Change your initializeSpinner() to add your object at the '0' th index -

 private void initializeSpinner(ArrayList<AtlasContact> atlastContacts) {

atlastContacts.add(0, new AtlasContact()); //Set the properties of the anonymous object to what you want.

    ArrayAdapter<AtlasContact> adapter = new ArrayAdapter<AtlasContact>(this,android.R.layout.simple_spinner_item, atlastContacts);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    spinnerAtlasContact.setAdapter(adapter); 
    }
Shadab Ansari
  • 7,022
  • 2
  • 27
  • 45