Wednesday, 28 December 2011

How To Create A Spinner With Data Bind In Android.



Create the Spinner code in "main.xml". This for the UI (spinner design).




  <RelativeLayout >
         <TextView
        android:id="@+id/textView1"
        android:layout_width="120dip"
         android:layout_marginTop="10dip"
        style="@style/FixAppointMentDettextView"
        android:layout_height="wrap_content"
        android:text="Blood Group" />
       
         </RelativeLayout>
       
     <LinearLayout android:layout_height="wrap_content" android:layout_width="180dip" >
    <Spinner
       
        android:id="@+id/add_pa_bloodgroup_spinner"      
        android:layout_width="180dip"
        android:textSize="90dip"
        android:layout_height="wrap_content" >
    </Spinner>
    </LinearLayout>


..............................................................................................................................................................


Create an array of data.... that display in the popup of the spinner.




res/values/String.xml




<string-array name="BloodGroup" > 
        <item>Select</item>
        <item>A+</item>
        <item>A-</item>
        <item>B+</item>
        <item>B-</item>
        <item>AB+</item>
        <item>AB-</item>
        <item>O+</item>
        <item>O-</item>
        </string-array>




....................................................................................................................................................................




Add the java code in the onCreate of .java






Spinner spinner = (Spinner) findViewById(R.id.add_pa_bloodgroup_spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
    this, R.array.BloodGroup, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);




....................................................................................................................................................................






Create A callback method in .java.





public class MyOnItemSelectedListener implements OnItemSelectedListener {


   public void onItemSelected(AdapterView<?> parent,
       View view, int pos, long id) {
    blood = parent.getItemAtPosition(pos).toString();    
    System.out.println("555555555VALUE Blood" + blood);
   }
   
   
  
   public void onNothingSelected(AdapterView parent) {



                        }
             }




..................................................................................................................................................................


Use this on the .java's onCreate part.




add_pa_bloodgroup_spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());




....................................................................................................................................

No comments:

Post a Comment