2

所以我已经设置了我的应用程序的一部分,用户可以从微调器中选择最多五个东西(总共五个微调器),现在我有按钮通过一个意图将选择发送到一个新活动,它们显示在一个新的活动,但我想在单击按钮时将选择存储到内部数据库中,然后用户可以从菜单中检索他们的选择,然后按下按钮显示数据。

 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();
    }

    }
4

2 回答 2

1

你的确切问题是什么?是的,可以在单击按钮时创建数据库。

于 2013-01-30T05:29:11.923 回答
1

如果要存储这些值;可以使用 SharedPreferences 代替 database ——

    SharedPreferences prefs = getApplicationContext().getSharedPreferences(
              "com.example.app", Context.MODE_PRIVATE);
    // To Edit the shared preferences-
    SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();

     editor.putString("IWI0", text0);
     editor.putString("IWI1", text1);
     editor.putString("IWI2", text2);
     editor.putString("IWI3", text3);
     editor.putString("IWI4", text4);
     editor.commit();

     // To retrieve the data--
     SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
     String restoredText = prefs.getString("IWI0", null);
     if (restoredText != null) 
     {
       // Do something
     }

编辑
参考 - http://developer.android.com/guide/topics/data/data-storage.html#pref

于 2013-01-30T05:32:22.967 回答