Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SQlite,如何将两个值传递给另一个活动?_Android_Sqlite_Android Activity_Android Intent_Spinner - Fatal编程技术网

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

Android SQlite,如何将两个值传递给另一个活动?,android,sqlite,android-activity,android-intent,spinner,Android,Sqlite,Android Activity,Android Intent,Spinner,我正在修改我的程序,从数据库中获取它的值,我遇到了一些问题,我有点困惑该怎么做。我已经被困了好几天了,我会感谢你的帮助 当我在微调器(从数据库填充)上选择一个项目时,我需要它传递INT值,以便将其发送到另一个活动 到目前为止,我的代码如下所示,数据库实用程序: 公共类数据库实用程序{ static final String DB_NAME="food"; static final String BEEF_TABLE="beef"; static final String CHICKEN_TABL

我正在修改我的程序,从数据库中获取它的值,我遇到了一些问题,我有点困惑该怎么做。我已经被困了好几天了,我会感谢你的帮助

当我在微调器(从数据库填充)上选择一个项目时,我需要它传递INT值,以便将其发送到另一个活动

到目前为止,我的代码如下所示,数据库实用程序:

公共类数据库实用程序{

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;
}
我需要抓取cookTime INT并将其传递给我的微调器活动:

package com.tsilo.grillbuddy;
导入android.app.Activity; 导入android.content.Intent; 导入android.os.Bundle; 导入android.view.view; 导入android.widget.Button; 导入android.widget.SimpleCursorAdapter; 导入android.widget.Spinner

公共课堂活动{

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

}
编辑:如您所见,我合并了该解决方案,我被要求将微调器更改为final以使其正常工作,并将DbUtility更改为db。它编译良好,没有错误,我可以在单击它时进入我的chicken活动,但现在,当我单击计时器按钮时,我会强制关闭,我的DDMS如下所示



编辑…好的,为您的TimerButton onClick()方法尝试此方法。您的SimpleCorsorAdapter将需要声明为“final”,但您的微调器不再需要是,您的数据库实用程序db实例也不需要是final

    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);
        }
    });
另外,不要在DBUtility类中使用getCookTime()方法,而是将其添加到ChickRecastivity类中。我添加了一些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;
    }

编辑…好的,为您的TimerButton onClick()方法尝试此方法。您的SimpleCorsOrAdapters将需要声明为“final”,但您的微调器不再需要是,您的DBUtility数据库实例也不必是final

    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);
        }
    });
另外,不要在DBUtility类中使用getCookTime()方法,而是将其添加到ChickRecastivity类中。我添加了一些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;
    }

我正在尝试解决这个问题。我在类中声明了int option1value,这是我将用来将它传递给putExtra的。我将代码放在我的chicken活动中的beefSpinner.setAdapter下。我不知道该将查询放在哪里,你能详细说明一下吗。这是我的第一个android应用程序和我的第一个数据库incorporated在一个程序中,所以我在这方面非常弱。我将我的字符串查询设置为:String query=“SELECT*FROM”+DbUtility.BEEF\u TABLE+“WHERE name=”“+selectedItem+”””。这有意义吗?抱歉,是的。然后你需要至少获取一个可读的数据库实例并执行该查询。这将返回一个“游标”(您可能需要使用SQLiteCursor),您可以使用getColumnIndex(“cookTime”)并将其用于getInt(int columnIndex)为了获得烹饪时间的价值。我仍然需要一些帮助,我在我的帖子中添加了一些东西,也许是关于如何获得它的想法。我不确定如何做到你所描述的。你介意展示一个例子吗?请看我的编辑-它不是完美的,只是给出了我试图表达的想法。我正在尝试解决这个问题。我有int option1value de在我的课堂上,克莱德说,这就是我要用来把它传递给putExtra的东西。我把代码放在我的鸡肉活动中的beefSpinner.setAdapter下。我不知道该把查询放在哪里,你能不能再详细说明一下。这是我的第一个android应用程序,也是我第一个整合到程序中的数据库,所以我在这方面很弱。我的String查询如下所示:String query=“SELECT*FROM”+DbUtility.BEEF_TABLE+“WHERE name=”“+selectedItem+””“这有意义吗?对不起,是的。然后您需要至少获取一个可读的数据库实例并执行该查询。这将返回一个“游标”(您可能希望使用SQLiteCursor),并且可以使用getColumnIndex(“cookTime”),并使用它来获取getInt(int columnIndex)的cookTime值。我仍然需要一些帮助,我在我的帖子中添加了一些东西,也许是关于如何获取它的想法。我不确定如何实现您所描述的。您介意展示一个示例吗?请参阅我的编辑-它并不完美,只是给出了我试图表达的想法。