私は Android で SQLite チュートリアルに従っていますが、今は困惑しています。
助けていただければ幸いです。:)
私のコードは、キッチンの在庫を表すデータベースを作成し、それに応じてアイテムを追加/削除することです。
私が使用したクラス:
KitchenItem - キッチンの在庫アイテムの
表現 KitchenDbHelper - SQLiteOpenHelper を拡張して DB を作成します
KitchenDataSource - DB でアイテムを実際に追加/削除します
KitchenInventoryActivity - レイアウトを設定し、KitchenDataSource を初期化します
しかし、KitchenInventoryActivity を実行しようとすると、例外が発生します:
Exception Ljava/util/MissingFormatArgumentException; LKitchenDbHelper の初期化中にスローされます。
Thais は、KitchenDataSource を新規作成する onCreate() の一部です。
より多くのログをキャプチャしたいのですが、残念ながらコンストラクターにログを追加できません。:(
ほとんどのコードは無関係だと思いますが、以下にコードを投稿します。
どんな助けや提案も大歓迎です!
以下のように、KitchenDbHelper のコード:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
//creates the DB
public class KitchenDbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "kitchen.db";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_KITCHEN = "kitchen";
//columns in database
public static final String KEY_ID = "id";
public static final String KEY_CATEGORY = "category";
public static final String KEY_ITEM = "item";
public static final String KEY_AMOUNT = "amount";
private static final String DATABASE_CREATE_STATEMENT =
String.format("CREATE_TABLE %s ( %s INTEGER PRIMARY KEY, %s TEXT, %s TEXT, %s TEXT;",
TABLE_KITCHEN,
KEY_ID,
KEY_CATEGORY,
KEY_AMOUNT);
public KitchenDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE_STATEMENT);
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
database.execSQL("DROP TABLE IF EXISTS " + TABLE_KITCHEN);
onCreate(database);
}
}
KitchenItem のコード:
import android.util.Log;
//this will be used as entries into KitchenDB
public class KitchenItem {
//TODO: this will probably be modified in the future
private long id;
private String category;
private String itemName;
private String amount;
public KitchenItem() {
}
public KitchenItem(int id, String category, String itemName, int amount) {
this.id = id;
this.category = category;
this.itemName = itemName;
this.amount = Integer.toString(amount);
}
public KitchenItem(String itemName, int amount) {
this.itemName = itemName;
this.amount = Integer.toString(amount);
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = Integer.toString(amount);
}
@Override
public String toString() {
Log.d("KITCHEN", "toString in KitchenItem");
return "itemName";
}
}
KitchenDataSource のコード:
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class KitchenDataSource {
private SQLiteDatabase database;
private KitchenDbHelper dbHelper;
private String[] allColumns = {KitchenDbHelper.KEY_ID,
KitchenDbHelper.KEY_CATEGORY,
KitchenDbHelper.KEY_ITEM,
KitchenDbHelper.KEY_AMOUNT};
public KitchenDataSource(Context context) {
dbHelper = new KitchenDbHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public KitchenItem addItem(KitchenItem item) {
ContentValues values = new ContentValues();
values.put(KitchenDbHelper.KEY_CATEGORY, item.getCategory());
values.put(KitchenDbHelper.KEY_ITEM, item.getItemName());
values.put(KitchenDbHelper.KEY_AMOUNT, item.getAmount());
long insertId = database.insert(KitchenDbHelper.TABLE_KITCHEN, null, values);
Cursor cursor = database.query(KitchenDbHelper.TABLE_KITCHEN,
allColumns,
KitchenDbHelper.KEY_ID + " = " + insertId,
null, null, null, null);
cursor.moveToFirst();
KitchenItem newKitchenItem = cursorToKitchenItem(cursor);
cursor.close();
return newKitchenItem;
}
public KitchenItem getItem(int id) {
Cursor cursor = database.query(KitchenDbHelper.TABLE_KITCHEN,
new String[] {allColumns[0], allColumns[1], allColumns[2], allColumns[3]},
KitchenDbHelper.KEY_ID + "=?",
new String[] {String.valueOf(id)},
null, null, null, null);
if(cursor != null) {
cursor.moveToFirst();
}
KitchenItem item = new KitchenItem(Integer.parseInt(cursor.getString(0)),
cursor.getString(1),
cursor.getString(2),
Integer.parseInt(cursor.getString(3)));
cursor.close();
return item;
}
public List<KitchenItem> getAllItems() {
List<KitchenItem> itemList = new ArrayList<KitchenItem>();
Cursor cursor = database.query(KitchenDbHelper.TABLE_KITCHEN,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while(!cursor.isAfterLast()) {
KitchenItem item = cursorToKitchenItem(cursor);
itemList.add(item);
cursor.moveToNext();
}
cursor.close();
return itemList;
}
public int updateItem(KitchenItem item) {
ContentValues values = new ContentValues();
values.put(KitchenDbHelper.KEY_CATEGORY, item.getCategory());
values.put(KitchenDbHelper.KEY_ITEM, item.getItemName());
values.put(KitchenDbHelper.KEY_AMOUNT, item.getAmount());
return database.update(KitchenDbHelper.TABLE_KITCHEN, values, KitchenDbHelper.KEY_ID + " = ?", new String[] {String.valueOf(item.getId())});
}
public void deleteItem(KitchenItem item) {
long id = item.getId();
database.delete(KitchenDbHelper.TABLE_KITCHEN,
KitchenDbHelper.KEY_ID + " = " + id,
null);
}
private KitchenItem cursorToKitchenItem(Cursor cursor) {
KitchenItem item = new KitchenItem();
item.setId(cursor.getLong(0));
item.setCategory(cursor.getString(1));
item.setItemName(cursor.getString(2));
item.setAmount(Integer.parseInt(cursor.getString(3)));
return item;
}
}
最後になりましたが、KitchenInventoryActivity:
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
public class KitchenInventoryActivity extends ListActivity {
private KitchenDataSource dataSource;
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d("Kitchen", "KitchenInventoryActivity onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.kitchen_inventory_activity);
dataSource = new KitchenDataSource(this);
Log.d("Kitchen", "dataSource initialized");
dataSource.open();
List<KitchenItem> itemList = dataSource.getAllItems();
ArrayAdapter<KitchenItem> adapter = new ArrayAdapter<KitchenItem>(this,
android.R.layout.simple_list_item_1,
itemList);
setListAdapter(adapter);
}
public void onClick(View clickedButton) {
@SuppressWarnings("unchecked")
ArrayAdapter<KitchenItem> adapter = (ArrayAdapter<KitchenItem>)getListAdapter();
KitchenItem item = null;
switch(clickedButton.getId()) {
case R.id.add:
//TODO: using a preconstructed item for now, will need to add user input
KitchenItem tempItem = new KitchenItem("Pizza", 5);
item = dataSource.addItem(tempItem);
adapter.add(item);
break;
case R.id.delete:
//TODO: just deleting the first item for now, will need to add selection
if(getListAdapter().getCount() > 0) {
item = (KitchenItem)getListAdapter().getItem(0);
dataSource.deleteItem(item);
adapter.remove(item);
}
break;
}
adapter.notifyDataSetChanged();
}
@Override
protected void onResume() {
dataSource.open();
super.onResume();
}
@Override
protected void onPause() {
dataSource.close();
super.onPause();
}
}