我希望从微调器中选择的值保存在内存或临时表或任何地方,以便它可以在下一个选项卡上使用。
我看了很多教程,但没有找到任何解决方案。
我有以下代码(它只给我 toast 消息;我从数据库中填充微调器):
super.onCreate(savedInstanceState);
setContentView(R.layout.customers);
addFromDB();
select_cust_name=(EditText)findViewById(R.id.select_cust_name);
}
private void addFromDB() {
// TODO Auto-generated method stub
final Spinner spinner = (Spinner) findViewById(R.id.spinner_db);
resultsView = (TextView) findViewById(R.id.textView);
SQLiteDatabase myDB = null;
myDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, 1, null);
// ////////////These are using to add the values to database
myDB.execSQL("CREATE TABLE " + SAMPLE_TABLE_NAME + " (" + _id
+ " INTEGER PRIMARY KEY AUTOINCREMENT , " + cust_name
+ " TEXT , " + cust_add + " TEXT)");
myDB.execSQL("insert into tbl_customer(cust_name, cust_add) values ('Fool', 'FF' );");
final Cursor c = myDB.query(SAMPLE_TABLE_NAME, null, null, null, null, null, null);
char _idColumnIndex = (char) c.getColumnIndexOrThrow("_id");
char cust_nameColumnIndex = (char) c.getColumnIndexOrThrow("cust_name");
char cust_addColumnIndex = (char) c.getColumnIndexOrThrow("cust_add");
adapterForSpinner = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item);
adapterForSpinner
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapterForSpinner);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// TODO Auto-generated method stub
SQLiteDatabase myDB = null;
Toast.makeText(
parent.getContext(),
"Customer is "
+ parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_LONG).show();
String selected = parent.getItemAtPosition(pos).toString();
insert(selected);
提前致谢。
这是我的日志:
05-03 11:29:31.743: E/Cursor(10155): Finalizing a Cursor that has not been deactivated or closed. database = /data/data/tab.layout/databases/db_sales, table = tbl_customer, query = SELECT * FROM tbl_customer
05-03 11:29:31.743: E/Cursor(10155): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
05-03 11:29:31.743: E/Cursor(10155): at android.database.sqlite.SQLiteCursor.<init>(SQLiteCursor.java:210)
05-03 11:29:31.743: E/Cursor(10155): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:53)
05-03 11:29:31.743: E/Cursor(10155): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
05-03 11:29:31.743: E/Cursor(10155): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1229)
05-03 11:29:31.743: E/Cursor(10155): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184)
05-03 11:29:31.743: E/Cursor(10155): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1264)
05-03 11:29:31.743: E/Cursor(10155): at tab.layout.Customers.addFromDB(Customers.java:63)
05-03 11:29:31.743: E/Cursor(10155): at tab.layout.Customers.onCreate(Customers.java:40)
05-03 11:29:31.743: E/Cursor(10155): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-03 11:29:31.743: E/Cursor(10155): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-03 11:29:31.743: E/Cursor(10155): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
05-03 11:29:31.743: E/Cursor(10155): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
05-03 11:29:31.743: E/Cursor(10155): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
05-03 11:29:31.743: E/Cursor(10155): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
05-03 11:29:31.743: E/Cursor(10155): at android.widget.TabHost.setCurrentTab(TabHost.java:323)
05-03 11:29:31.743: E/Cursor(10155): at android.widget.TabHost.addTab(TabHost.java:213)
05-03 11:29:31.743: E/Cursor(10155): at tab.layout.MyTablayoutActivity.SetupTabs(MyTablayoutActivity.java:48)
05-03 11:29:31.743: E/Cursor(10155): at tab.layout.MyTablayoutActivity.onCreate(MyTablayoutActivity.java:20)
05-03 11:29:31.743: E/Cursor(10155): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-03 11:29:31.743: E/Cursor(10155): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-03 11:29:31.743: E/Cursor(10155): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-03 11:29:31.743: E/Cursor(10155): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-03 11:29:31.743: E/Cursor(10155): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-03 11:29:31.743: E/Cursor(10155): at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 11:29:31.743: E/Cursor(10155): at android.os.Looper.loop(Looper.java:123)
05-03 11:29:31.743: E/Cursor(10155): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-03 11:29:31.743: E/Cursor(10155): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 11:29:31.743: E/Cursor(10155): at java.lang.reflect.Method.invoke(Method.java:521)
05-03 11:29:31.743: E/Cursor(10155): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-03 11:29:31.743: E/Cursor(10155): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-03 11:29:31.743: E/Cursor(10155): at dalvik.system.NativeStart.main(Native Method)