我正在修改我的程序以从数据库中获取其值,我遇到了一些问题而且我对该怎么做有点困惑。我已经被困了好几天了,我很感激你的帮助。
当我在我的微调器上选择一个项目(从数据库填充)时,我需要它传递INT值,以便我可以将它发送到另一个活动。
到目前为止,我的代码看起来像这样,数据库实用程序:
公共类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;
}
我需要抓住,cookTime INT并将其传递给我的微调器活动:
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;
公共类ChickenActivity扩展了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);
}
});
}
编辑:正如您所看到我合并了解决方案,我被要求将我的微调器更改为最终,以便他们工作并将DbUtility更改为db。它编译好没有错误,当我点击它时,我能够进入我的鸡活动,但是当我点击我的计时器按钮时,我现在得到一个力,我的DDMS看起来如下
答案 0 :(得分:1)
编辑... 好的,请尝试使用TimerButton onClick()方法。您的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);
}
});
此外,不是在DBUtility类中使用getCookTime()方法,而是将其添加到ChickenActivity类中。我已经添加了一些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;
}