android-studio
  1. android-studio-sqlite-spinner

Android Studio SQLite Spinner

Syntax

public class MainActivity extends AppCompatActivity {

    private Spinner spinner;
    private ArrayList<String> categories;
    private ArrayAdapter<String> dataAdapter;
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinner = (Spinner) findViewById(R.id.spinner);
        categories = new ArrayList<String>();

        // create database and table if not exist
        createDatabase();

        // add data to spinner
        addDataToSpinner();

        // set spinner listener
        setSpinnerListener();
    }

    private void createDatabase() {
        db = openOrCreateDatabase("DatabaseName", Context.MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS Categories (id INTEGER PRIMARY KEY AUTOINCREMENT, category_name VARCHAR);");
    }

    private void addDataToSpinner() {
        // get data from the table
        Cursor c = db.rawQuery("SELECT category_name FROM Categories ORDER BY id ASC", null);
        if (c.moveToFirst()) {
            do {
                categories.add(c.getString(c.getColumnIndex("category_name")));
            } while (c.moveToNext());
        }
        c.close();

        // add data adapter to spinner
        dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataAdapter);
    }

    private void setSpinnerListener() {
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String category = parent.getItemAtPosition(position).toString();
                Toast.makeText(parent.getContext(), "Selected: " + category, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
}

Example

Let's create a simple spinner and populate it with data from a SQLite database table.

<Spinner
    android:id="@+id/spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp" />

Output

When the app is launched, the spinner will be populated with data from the Categories table in the SQLite database.

Android Studio SQLite Spinner Output

Explanation

We first create and execute a CREATE TABLE statement to create the Categories table if it doesn't exist. We then retrieve data from the table using a SQL SELECT statement and add it to an ArrayList. Finally, we create an ArrayAdapter and set it as the data adapter for the spinner.

We also set a listener for the spinner so that we can handle the user selecting an item from the dropdown menu.

Use

A spinner is a view that allows users to select one item from a dropdown list. It is often used to provide a selection of options for the user to choose from.

SQLite is a lightweight database that is built into Android. It is often used to store application data on the device.

By combining the spinner and SQLite, you can create a dynamic view that allows users to select data from a database.

Important Points

  • Create the SQLite database and table if they don't exist.
  • Retrieve data from the table using a SQL SELECT statement.
  • Add the data to an ArrayList and create an ArrayAdapter for the spinner.
  • Set a listener for the spinner to handle user selections.

Summary

In this tutorial, we learned how to create a spinner in Android Studio and populate it with data from a SQLite database table. By combining the spinner and SQLite, we can create a dynamic view that allows users to select data from a database.

Published on: