SQLite Spinner

August 3,2016

In this example, we are adding a label on button click and displaying all the added labels on the spinner. As you have seen in the previous example, SQLiteOpenHelper class need to be extended for performing operations on the sqlite.

We have overridden the onCreate() and onUpgrade() method of SQLiteOpenHelper class in the DatabaseHandler class that provides additional methods to insert and display the labels or data.

Android Sqlite Spinner Example

Let’s see the simple code to add and display the string content on spinner using sqlite database.

activity_main.xml

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass=http://schemas.android.com/apk/res/android&#8221;
  2.     xmlns:tools=http://schemas.android.com/tools&#8221;
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     tools:context=“.MainActivity” >
  6.      <!– Label –>
  7.     <TextView
  8.         android:layout_width=“fill_parent”
  9.         android:layout_height=“wrap_content”
  10.         android:text=“Add New Label”
  11.         android:padding=“8dip” />
  12.     <!– Input Text –>
  13.     <EditText android:id=“@+id/input_label”
  14.         android:layout_width=“fill_parent”
  15.         android:layout_height=“wrap_content”
  16.         android:layout_marginLeft=“8dip”
  17.         android:layout_marginRight=“8dip”/>
  18.     <Spinner
  19.         android:id=“@+id/spinner”
  20.         android:layout_width=“fill_parent”
  21.         android:layout_height=“wrap_content”
  22.         android:layout_alignParentLeft=“true”
  23.         android:layout_below=“@+id/btn_add”
  24.         android:layout_marginTop=“23dp” />
  25.     <Button
  26.         android:id=“@+id/btn_add”
  27.         android:layout_width=“wrap_content”
  28.         android:layout_height=“wrap_content”
  29.         android:layout_below=“@+id/input_label”
  30.         android:layout_centerHorizontal=“true”
  31.         android:text=“Add Item” />
  32. </RelativeLayout>

Leave a comment