Hold Spinner value for next tab

I want the selected value from a spinnner to be kept in memory or temp table or anywhere, so that it can be used on next tab. I watched lot of tutorials, but didn't find any solution. I've the following code (it gives me toast message only; I populate the spinner from a database):

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

Thanks in advance.

here is my log:

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)

Answers


A couple options...

Global Variable

Declare it in your main activity

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

Sett from anywhere...

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

Then you can just use Main.myString in whatever your next activity/tab/fragment is.

You didn't say how you were doing the tabs (if they were a new activity or not) so another possible way is to pass it through an intent extra.

Pass in intent

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

then in the new activity retrieve it like so:

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

Hope this helps.


I think the error is related to database helper class. can u paste the method you have written for selecting contents from the table...


or in your method close the cursor and also db at the end db.close(); if (cursor != null && !cursor.isClosed()) { cursor.close(); }


Need Your Help

Calling super's super method

ruby

Is is possible to do something like super.super in the overriden method? That is, to bypass the direct parent's super and call "grandparent's" super?

How to run periodic processes on SQL Server 2008

c# asp.net sql sql-server-2008

I've built a very simple chatroom-like ASP.NET application which displays current Online/Offline users:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.