そのため、ユーザーがスピナーから最大 5 つのもの (合計 5 つのスピナー) を選択するアプリの一部をセットアップしました。現在、インテントを介して選択内容を新しいアクティビティに送信するボタンがあり、新しいアクティビティに表示されます。アクティビティですが、ボタンのクリック時に選択内容を内部データベースに保存したいので、ユーザーはボタンの表示データを押してメニュー購入から選択内容を取得できます。
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);
}
});
}
}
このクラスはデータを新しい画面に送信します。
以下のクラスは私のヘルパークラスです
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();
}
}