So I have set up part of my app where a user will select up to five things from from spinners (five spinners in total) and right now I have the button sending the selections to a new activity via an intent and they display on a new activity but I want to store the selections to an internal database on the button click and then the user can retrieve their selections from the menu buy pushing the button show data.
public class IwiSelect extends Activity {
Spinner spinner1, spinner2, spinner3, spinner4, spinner5;
Button btnSubmit;
String text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_iwiselect);
addListenerOnButton();
}
// get the selected drop down list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner3 = (Spinner) findViewById(R.id.spinner3);
spinner4 = (Spinner) findViewById(R.id.spinner4);
spinner5 = (Spinner) findViewById(R.id.spinner5);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
String text0 = spinner1.getSelectedItem().toString();
String text1 = spinner2.getSelectedItem().toString();
String text2 = spinner3.getSelectedItem().toString();
String text3 = spinner4.getSelectedItem().toString();
String text4 = spinner5.getSelectedItem().toString();
//Starting a new Intent
Intent intent = new Intent(IwiSelect.this, SecondScreenActivity.class);
//Sending data to another Activity
intent.putExtra("IWI0", text0);
intent.putExtra("IWI1", text1);
intent.putExtra("IWI2", text2);
intent.putExtra("IWI3", text3);
intent.putExtra("IWI4", text4);
// starting new activity
startActivity(intent);
}
});
}
}
this class sends the data to the new screen.
the class below is my helper class
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "iwi";
// table name
private static final String TABLE_IWI = "iwis";
// Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "iwiname";
public DatabaseHandler(SecondScreenActivity secondScreenActivity) {
super((Context) secondScreenActivity, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_IWI_TABLE = "CREATE TABLE " + TABLE_IWI + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ " TEXT" + ")";
db.execSQL(CREATE_IWI_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_IWI);
// Create tables again
onCreate(db);
}
// Deleting single contact
public void deleteIwi(Iwi iwi) {}
// Adding new contact
public void addIwi(Iwi iwi) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, iwi.getName());
// Inserting Row
db.insert(TABLE_IWI, null, values);
db.close(); // Closing database connection
}
// Getting single contact
public Iwi getIwi(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_IWI, new String[] { KEY_ID,
KEY_NAME, }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Iwi iwi = new Iwi(Integer.parseInt(cursor.getString(0)),
cursor.getString(1));
// return contact
return iwi;
}
// Getting All Contacts
public List<Iwi> getAllIwi() {
List<Iwi> iwiList = new ArrayList<Iwi>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_IWI;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Iwi iwi = new Iwi();
iwi.setID(Integer.parseInt(cursor.getString(0)));
iwi.setName(cursor.getString(1));
// Adding contact to list
iwiList.add(iwi);
} while (cursor.moveToNext());
}
// return contact list
return iwiList;
}
//Getting contacts Count
public int getIwiCount() {
String countQuery = "SELECT * FROM " + TABLE_IWI;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
// Updating single contact
public int updateIwi(Iwi iwi) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, iwi.getName());
// updating row
return db.update(TABLE_IWI, values, KEY_ID + " = ?",
new String[] { String.valueOf(iwi.getID()) });
}
// Deleting single contact
public void deleteContact(Iwi iwi) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_IWI, KEY_ID + " = ?",
new String[] { String.valueOf(iwi.getID()) });
db.close();
}
}
If you want to store these values ; Instead of database , SharedPreferences can be used-
Edit :
Reference-
http://developer.android.com/guide/topics/data/data-storage.html#pref