简体   繁体   English

Android SQlite,如何将两个值传递给另一个活动?

[英]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. 当我在微调器上选择一个项目(从数据库填充)时,我需要它与它一起传递INT值,以便可以将其发送到另一个活动。

So far my code looks like this, the database utility: 到目前为止,我的代码看起来像数据库实用程序:

public class DbUtility { 公共类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: 我需要抓紧cookTime INT并将其传递给我的微调器活动:

package com.tsilo.grillbuddy;

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

public class ChickenActivity extends Activity { 公共类ChickenActivity扩展了活动{

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. 编辑 :如您所见,我合并了解决方案,因此我需要将微调器更改为最终版本,以使它们正常工作,并将DbUtility更改为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 它可以正常编译,没有错误,单击它可以启动鸡的活动,但是单击计时器按钮后,我现在可以强制关闭,DDMS如下所示


替代文字

EDIT... OK, try this for your TimerButton onClick() method. 编辑...确定,为您的TimerButton onClick()方法尝试此操作。 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. 您的SimpleCursorAdapters将需要声明为“ final”,但Spinners不再需要是ChickenActivity中的DBUtility数据库实例。

    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. 另外,不要在DBUtility类中使用getCookTime()方法,而应将其添加到ChickenActivity类中。 I've added some android.util.Log calls so you can test it's working the way you need it. 我添加了一些android.util.Log调用,以便您可以测试它是否按需要工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据/参数传递给 Android 中的另一个活动 - How do you pass data/parameters to another activity in Android 如何将活动中的值传递给 Android Studio 中的 class? - How do you pass values from an activity to a class in Android Studio? 如何在Android中的另一个活动中将两个edittext值传递给listview? - How to pass two edittext values to listview in another activity in android? Android:如何将回调传递给活动? - Android: How do you pass a callback to an activity? 如何将数据从sqlite数据库传递到另一个活动? - How do I pass data from sqlite database to another activity? 如何在Android中将两个字符串从一个活动传递到另一个活动 - How to pass two Strings from one Activity to another Activity in Android Android-如何将Textview值从Activity-A传递到适配器 - Android - How do you pass textview values from Activity-A to an adapter 最好是将数据传递到另一个活动或对sqlite进行两次查询 - What's best pass data to another activity or do two queries to sqlite Android:如何不断更新/传递值从一个活动到另一个活动? - Android: How to constantly update/pass values from one activity to another? 如何将活动值传递给另一个活动(科特琳) - How to pass activity values to another activity (Kotlin)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM