简体   繁体   中英

Android SQlite, How do you pass two values to another activity?

I am modifying my program to get its values from a database and I am having some issues and I am a little bit confused on what to do. I've been stuck for days, I would appreciate the help.

When I select an item on my spinner(populated from database), I need it to pass the INT value along with it so I can send it to another activity.

So far my code looks like this, the database utility:

public class DbUtility {

static final String DB_NAME="food";
static final String BEEF_TABLE="beef";
static final String CHICKEN_TABLE="chicken";

SQLiteDatabase db=null; 
Context context;

public static class DatabaseHelper extends SQLiteOpenHelper
{
    public DatabaseHelper(Context context, String name, CursorFactory factory, int version) 
    {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }
    @Override
    public void onCreate(SQLiteDatabase db) 
    {           
        db.execSQL("CREATE TABLE IF NOT EXISTS BEEF_TABLE (_id INT PRIMARY KEY,name VARCHAR, cookTime INT);");
        db.execSQL("INSERT INTO BEEF_TABLE values(1,'Skirt Steak', 23000);");
        db.execSQL("INSERT INTO BEEF_TABLE values(2,'Flank Steak',23000);");
        db.execSQL("INSERT INTO BEEF_TABLE values(3,'Filet Mignon',23000);");
        db.execSQL("CREATE TABLE IF NOT EXISTS CHICKEN_TABLE (_id INT PRIMARY KEY,name VARCHAR, cookTime INT);");
        db.execSQL("INSERT INTO CHICKEN_TABLE values(1,'Breast',20000);");
        db.execSQL("INSERT INTO CHICKEN_TABLE values(2,'Wings',10000);");
        db.execSQL("INSERT INTO CHICKEN_TABLE values(3,'Burger',30000);");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
    }   
}
public DbUtility(Context context) {
    this.context=context;
}
public SimpleCursorAdapter getBeefAdapter()
{
    DatabaseHelper dbhelper=new DatabaseHelper(this.context, DB_NAME, null, 1);
    db=dbhelper.getWritableDatabase();

    Cursor beefCursor=db.rawQuery("SELECT * FROM BEEF_TABLE", null);
    while(!beefCursor.isLast())
    beefCursor.moveToNext(); //I don't understand why but it's necessary (alternative call c.getCount() )
    String[] beefType=new String[1];
    int[] to=new int[1];
    beefType[0]="name";
    to[0]=android.R.id.text1;
    SimpleCursorAdapter beefAdapter=new SimpleCursorAdapter(this.context, android.R.layout.simple_spinner_item, beefCursor, beefType, to);
    beefAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    db.close();
    return beefAdapter; 

}   


public SimpleCursorAdapter getChickenAdapter()
{
    DatabaseHelper dbhelper=new DatabaseHelper(this.context, DB_NAME, null, 1);
    db=dbhelper.getWritableDatabase();

    Cursor chickenCursor=db.rawQuery("SELECT * FROM CHICKEN_TABLE", null);
    while(!chickenCursor.isLast())
    chickenCursor.moveToNext(); //I don't understand why but it's necessary (alternative call c.getCount() )
    String[] chickenType=new String[1];
    int[] type=new int[1];
    chickenType[0]="name";
    type[0]=android.R.id.text1;
    SimpleCursorAdapter chickenAdapter=new SimpleCursorAdapter(this.context, android.R.layout.simple_spinner_item, chickenCursor, chickenType, type);
    chickenAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    db.close();
    return chickenAdapter;  
}   

public int getCookTime(String theTable, String theFood) 
{
    DatabaseHelper dbhelper=new DatabaseHelper(this.context, DB_NAME, null, 1);
    SQLiteDatabase db=dbhelper.getReadableDatabase();
    int TheCookTime = 0;
    Cursor foodCursor = db.rawQuery("SELECT * FROM " + theTable + " WHERE name='" + theFood + "'", null);
    TheCookTime = foodCursor.getInt(foodCursor.getColumnIndex("cookTime"));
    return TheCookTime;
}

I need to grab, cookTime INT and pass it to my spinner activity:

package com.tsilo.grillbuddy;

import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.SimpleCursorAdapter; import android.widget.Spinner;

public class ChickenActivity extends Activity {

int option1value;
int option2value;
/** Called when the activity is first created. */
@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.spinner);

    final DbUtility db=new DbUtility(this);


    SimpleCursorAdapter beefAdapter=db.getBeefAdapter();
    final Spinner beefSpinner=(Spinner)this.findViewById(R.id.spinner);
    beefSpinner.setAdapter(beefAdapter);

    SimpleCursorAdapter chickenAdapter=db.getChickenAdapter();
    final Spinner chickenSpinner=(Spinner)this.findViewById(R.id.spinner2);
    chickenSpinner.setAdapter(chickenAdapter);

    Button TimerButton = (Button)findViewById(R.id.TimerActivityButton);
    TimerButton.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v)
        {
            String beefSelection = (String) beefSpinner.getSelectedItem();
            option1value = db.getCookTime(DbUtility.BEEF_TABLE, beefSelection);
            String chickenSelection = (String) chickenSpinner.getSelectedItem();
            option2value = db.getCookTime(DbUtility.CHICKEN_TABLE, chickenSelection);
            Intent timer = new Intent (ChickenActivity.this,TimerActivity.class);
            timer.putExtra("option1", option1value);
            timer.putExtra("option2", option2value);
            startActivity(timer);
        }
    });

}

Edit : As you can see I incorporated the solution, I was required to change my spinners to final for them to work and change DbUtility to db. It compiles fine with no errors, and I am able to get to my chicken activity when I click it, but I now get a force close when I click my Timer Button, my DDMS looks as follows


替代文字

EDIT... OK, try this for your TimerButton onClick() method. Your SimpleCursorAdapters will need to be declared as 'final' but your Spinners no longer need to be neither does your DBUtility db instance in the ChickenActivity.

    TimerButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            option1value = getCookTime(beefAdapter);
            option2value = getCookTime(chickenAdapter);

            Intent timer = new Intent (ChickenActivity.this, TimerActivity.class);
            timer.putExtra("option1", option1value);
            timer.putExtra("option2", option2value);
            startActivity(timer);
        }
    });

Also, instead of having the getCookTime() method in the DBUtility class, add this to the ChickenActivity class instead. I've added some android.util.Log calls so you can test it's working the way you need it.

    public int getCookTime(SimpleCursorAdapter adapter) {
        String theName = "None";
        int theCookTime = 0;

        Cursor theCursor = adapter.getCursor();
        theName = theCursor.getString(theCursor.getColumnIndex("name"));
        theCookTime = theCursor.getInt(theCursor.getColumnIndex("cookTime"));
        android.util.Log.i("ChickenActivity", "theName: " + theName);
        android.util.Log.i("ChickenActivity", "theCookTime: " + theCookTime);
        return theCookTime;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM