保持下一个选项卡的微调器值

时间:2012-05-03 05:01:28

标签: android memory tabs spinner

我希望将一个spinnner中的选定值保存在内存或临时表或任何地方,以便可以在下一个选项卡上使用。
我看了很多教程,但没有找到任何解决方案 我有以下代码(它只给我吐司信息;我从数据库填充微调器):

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)

3 个答案:

答案 0 :(得分:3)

几个选项......

全局变量

在您的主要活动中声明它

public class Main extends Activity {
    public static String myString;  // declare global variable
}

从任何地方安排......

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
         Main.myString = parent.getItemAtPosition(pos).toString();
}

然后,您可以在下一个活动/标签/片段中使用Main.myString。

你没有说明你是如何做标签的(如果它们是一项新的活动),所以另一种可能的方法是通过额外的意图传递它。

传递意图

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
    myIntent.putExtra("Selected", parent.getItemAtPosition(pos).toString());
    CurrentActivity.this.startActivity(myIntent);
}

然后在新活动中检索它:

Bundle extras = getIntent().getExtras();
String selected = extras.getString("Selected");

希望这有帮助。

答案 1 :(得分:0)

我认为错误与数据库助手类有关。你可以粘贴你写的方法来从表格中选择内容......

答案 2 :(得分:0)

或在你的方法中关闭光标并在末尾的数据库中    db.close();
if(cursor!= null&amp;&amp;!cursor.isClosed()){
 cursor.close();
}