A.class にあるリストビューにデータを挿入しようとしています。データは B.class の編集テキストから取得されます。2 つのテーブルがあります。コードは次のとおりです。
これは私のDbAdapterです
public class SQLiteAdapter extends Search{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.row);
}
public static final String APP_NAME = "AdvancedSearch";
public static final String MYDATABASE_NAME = "Food_DB";
public static final String MYDATABASE_TABLE = "Food_Table";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_ID = "_id";
public static final String KEY_FOODNAME = "FoodName";
public static final String KEY_CALORIES = "Calories";
public static final String FOODLOG = "Food_tab";
public static final String ID = "_id";
public static final String FOODNAME = "FoodName";
public static final String CALORIES = "Calories";
//create table MY_DATABASE (ID integer primary key, Content text not null);
private static final String SCRIPT_CREATE_DATABASE =
"create table " + MYDATABASE_TABLE + " ("
+ KEY_ID + " integer primary key autoincrement, "
+ KEY_FOODNAME + " text not null, " +
KEY_CALORIES + " integer);";
private static final String CREATE_FOODLOG =
"create table " + FOODLOG + " ("
+ ID + " integer primary key autoincrement, "
+ FOODNAME + " text not null, " +
CALORIES + " integer);";
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;
private Context context;
public SQLiteAdapter(Context c){
context = c;
}
public SQLiteAdapter openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}
public SQLiteAdapter openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;
}
public void close()
{
sqLiteHelper.close();
}
public long insert(String foodname, int i){
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_FOODNAME, foodname);
contentValues.put(KEY_CALORIES, i);
return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}
public long insert1(String food, String cal){
ContentValues Values = new ContentValues();
Values.put(FOODNAME, food);
Values.put(CALORIES, cal);
return sqLiteDatabase.insert(FOODLOG, null, Values);
}
public int deleteAll(){
return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
}
public Cursor queueAll(){
String[] columns = new String[]{KEY_ID, KEY_FOODNAME, KEY_CALORIES};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
null, null, null, null, null);
return cursor;
}
public Cursor queueFoodLog(){
String[] columns = new String[]{ID, FOODNAME, CALORIES};
Cursor cursor = sqLiteDatabase.query(FOODLOG, columns,
null, null, null, null, null);
return cursor;
}
public void update_byID(int id, String v1, String v2){
ContentValues values = new ContentValues();
values.put(KEY_FOODNAME, v1);
values.put(KEY_CALORIES, v2);
sqLiteDatabase.update(MYDATABASE_TABLE, values, KEY_ID+"="+id, null);
}
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
db.execSQL(CREATE_FOODLOG);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
public void open()
{
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
}
public void delete_byID(int item_id) {
// TODO Auto-generated method stub
sqLiteDatabase.delete(MYDATABASE_TABLE, KEY_ID, null);
}
}
これが私のデータを取得しようとしているコードです。私のBクラス
private ListView.OnItemClickListener listContentOnItemClickListener = new ListView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Cursor cursor = (Cursor) parent.getItemAtPosition(position);
String item_content1 = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_FOODNAME));
String item_content2 = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CALORIES));
arlene = arlene + Integer.parseInt(item_content2);
String item = String.valueOf(" Food Name: " + item_content1 ) + "\n" +
" Calories: " + item_content2;
Toast.makeText(Search.this, item, Toast.LENGTH_LONG).show();
food.setText(item_content1);
calories.setText(String.valueOf(arlene));
String data1 = food.getText().toString();
String data2 = calories.getText().toString();
mySQLiteAdapter.insert1(data1, data2);
updateList();
}
これは私の A.class で、B.class に挿入されたデータを呼び出そうとしています
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
Cursor cursor = mySQLiteAdapter.queueAll();
startManagingCursor(cursor);
String[] from = new String[]{SQLiteAdapter.FOODNAME,SQLiteAdapter.CALORIES};
int[] to = new int[]{R.id.tv3, R.id.tv4};
SimpleCursorAdapter cursorAdapter =
new SimpleCursorAdapter(this, R.layout.foodlist, cursor, from, to);
foodlog.setAdapter(cursorAdapter);
これは私のLOGCATです
03-08 03:58:06.068: E/AndroidRuntime(27320): FATAL EXCEPTION: main
03-08 03:58:06.068: E/AndroidRuntime(27320): java.lang.IllegalStateException: database not open
03-08 03:58:06.068: E/AndroidRuntime(27320): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1493)
03-08 03:58:06.068: E/AndroidRuntime(27320): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
03-08 03:58:06.068: E/AndroidRuntime(27320): at me.mojica.caloriewatch.SQLiteAdapter.insert1(SQLiteAdapter.java:88)
03-08 03:58:06.068: E/AndroidRuntime(27320): at me.mojica.caloriewatch.Search$1.onItemClick(Search.java:230)
03-08 03:58:06.068: E/AndroidRuntime(27320): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
03-08 03:58:06.068: E/AndroidRuntime(27320): at android.widget.ListView.performItemClick(ListView.java:3382)
03-08 03:58:06.068: E/AndroidRuntime(27320): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
03-08 03:58:06.068: E/AndroidRuntime(27320): at android.os.Handler.handleCallback(Handler.java:587)
03-08 03:58:06.068: E/AndroidRuntime(27320): at android.os.Handler.dispatchMessage(Handler.java:92)
03-08 03:58:06.068: E/AndroidRuntime(27320): at android.os.Looper.loop(Looper.java:123)
03-08 03:58:06.068: E/AndroidRuntime(27320): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-08 03:58:06.068: E/AndroidRuntime(27320): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 03:58:06.068: E/AndroidRuntime(27320): at java.lang.reflect.Method.invoke(Method.java:521)
03-08 03:58:06.068: E/AndroidRuntime(27320): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-08 03:58:06.068: E/AndroidRuntime(27320): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-08 03:58:06.068: E/AndroidRuntime(27320): at dalvik.system.NativeStart.main(Native Method)
助けてください