C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Android Sqlite Example (with Spinner)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 ExampleLet's see the simple code to add and display the string content on spinner using sqlite database. activity_main.xmlFile: activity_main.xml
Activity classFile: MainActivity.java
package example.TheDeveloperBlog.com.sqlitespinner; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import android.widget.Toast; import java.util.List; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener { Spinner spinner; Button btnAdd; EditText inputLabel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner = findViewById(R.id.spinner); btnAdd = findViewById(R.id.btn_add); inputLabel = findViewById(R.id.input_label); spinner.setOnItemSelectedListener(this); // Loading spinner data from database loadSpinnerData(); btnAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { String label = inputLabel.getText().toString(); if (label.trim().length() > 0) { DatabaseHandler db = new DatabaseHandler(getApplicationContext()); db.insertLabel(label); // making input filed text to blank inputLabel.setText(""); // Hiding the keyboard InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(inputLabel.getWindowToken(), 0); // loading spinner with newly added data loadSpinnerData(); } else { Toast.makeText(getApplicationContext(), "Please enter label name", Toast.LENGTH_SHORT).show(); } } }); } /** * Function to load the spinner data from SQLite database * */ private void loadSpinnerData() { DatabaseHandler db = new DatabaseHandler(getApplicationContext()); List DatabaseHandler classFile: DatabaseHandler.java
package example.TheDeveloperBlog.com.sqlitespinner; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import java.util.ArrayList; import java.util.List; public class DatabaseHandler extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; private static final String DATABASE_NAME = "spinnerExample"; private static final String TABLE_NAME = "labels"; private static final String COLUMN_ID = "id"; private static final String COLUMN_NAME = "name"; public DatabaseHandler(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } // Creating Tables @Override public void onCreate(SQLiteDatabase db) { // Category table create query String CREATE_ITEM_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_NAME + " TEXT)"; db.execSQL(CREATE_ITEM_TABLE); } // Upgrading database @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // Drop older table if existed db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); // Create tables again onCreate(db); } /** * Inserting new lable into lables table * */ public void insertLabel(String label){ SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(COLUMN_NAME, label);//column name, column value // Inserting Row db.insert(TABLE_NAME, null, values);//tableName, nullColumnHack, CotentValues db.close(); // Closing database connection } /** * Getting all labels * returns list of labels * */ public List Output:
Next TopicAndroid XML Parsing Using Sax Parser
|