料理用のアプリを作っています。3つのSQLデータベースを作成し、リストにタブビューを入力しました。これはデータベースです
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class Cook_tab_mains_data extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "mains";
public Cook_tab_mains_data(Context context) {
super(context, DATABASE_NAME, null, 2);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE IF NOT EXISTS mains (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name TEXT, " +
"disc TEXT, " +
"photo TEXT, " +
"prep TEXT, " +
"thumb TEXT, " +
"ingre TEXT, " +
"howto TEXT, " +
"info TEXT, " +
"mainId INTEGER)";
db.execSQL(sql);
ContentValues values = new ContentValues();
values.put("name", "Name 3");
values.put("disc", "here is the description");
values.put("photo", "stub.png");
values.put("ingre", "the ingredients of the mains");
values.put("howto", "how to make this thing");
values.put("info", "basically its this much calorie and such and such");
db.insert("mains", "name", values);
values.put("name", "Name 4");
values.put("disc", "here is the description");
values.put("photo", "stub.png");
values.put("thumb", "ic_launcher.png");
values.put("ingre", "the ingredients of the mains");
values.put("howto", "how to make this thing");
values.put("info", "basically its this much calorie and such and such");
db.insert("mains", "name", values);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS mains");
onCreate(db);
}}
ご覧のとおり、私のデータベースは静的です。これで、アイテムを一覧表示するリストアクティビティと、アイテムの詳細を示す別のアクティビティがあります。
import java.io.IOException;
import java.io.InputStream;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class Cook_tab_mains extends ListActivity {
protected EditText searchText;
protected SQLiteDatabase db;
protected Cursor cursor;
protected ListAdapter adapter;
class CustomSimpleCursor extends SimpleCursorAdapter {
public CustomSimpleCursor(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
ImageView thumb = (ImageView) view.findViewById(R.id.thumb);
try {
InputStream bitmap = getAssets()
.open(cursor.getString(cursor
.getColumnIndexOrThrow("thumb")));
Bitmap bit = BitmapFactory.decodeStream(bitmap);
thumb.setImageBitmap(bit);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cook_tab_general);
db = (new Cook_tab_mains_data(this)).getWritableDatabase();
searchText = (EditText) findViewById (R.id.searchText);
EditText searchTo = (EditText)findViewById(R.id.searchText);
searchTo.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// autoclick
}
});
// || Query SQLite
cursor = db.rawQuery("SELECT _id, name, disc, thumb, prep FROM mains WHERE name LIKE ?",
new String[]{"%" + searchText.getText().toString() + "%"});
adapter = new CustomSimpleCursor(
this,
R.layout.cook_tab_generalist,
cursor,
new String[] {"name", "disc", "prep"},
new int[] {R.id.name, R.id.disc, R.id.prep});
setListAdapter(adapter);}
public void search(View view) {
// ||Query SQLite
cursor = db.rawQuery("SELECT _id, name, disc, thumb, prep FROM mains WHERE name LIKE ?",
new String[]{"%" + searchText.getText().toString() + "%"});
adapter = new CustomSimpleCursor(
this,
R.layout.cook_tab_generalist,
cursor,
new String[] {"name", "disc", "prep"},
new int[] {R.id.name, R.id.disc, R.id.prep});
setListAdapter(adapter);}
public void onListItemClick(ListView parent, View view, int position, long id) {
Intent intent = new Intent(this, Cook_tab_mains_details.class);
Cursor cursor = (Cursor) adapter.getItem(position);
intent.putExtra("MAINS_ID", cursor.getInt(cursor.getColumnIndex("_id")));
startActivity(intent);
}
}
次に、アイテムを選択すると、このアクティビティの詳細が表示されます。
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class Cook_tab_mains_details extends Activity {
protected TextView Name;
protected ImageView Photo;
protected TextView Ingredients;
protected TextView HowTo;
protected TextView Information;
protected int mainId;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cook_tab_generaldetails);
mainId = getIntent().getIntExtra("MAINS_ID", 0);
SQLiteDatabase db = (new Cook_tab_mains_data(this)).getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT emp._id, emp.name, emp.photo, emp.ingre, emp.howto, emp.info, emp.mainId, mgr.name mainsname, mgr.disc mainsdiscname FROM mains emp LEFT OUTER JOIN mains mgr ON emp.mainId = mgr._id WHERE emp._id = ?",
new String[]{""+mainId});
if (cursor.getCount() == 1)
{
cursor.moveToFirst();
Name = (TextView) findViewById(R.id.FoodName);
Name.setText(cursor.getString(cursor.getColumnIndex("name")));
Photo = (ImageView) findViewById(R.id.Photo);
try {
InputStream bitmap=getAssets().open(cursor.getString(cursor.getColumnIndexOrThrow("photo")));
Bitmap bit=BitmapFactory.decodeStream(bitmap);
Photo.setImageBitmap(bit);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Ingredients = (TextView) findViewById(R.id.Ingre);
Ingredients.setText(cursor.getString(cursor.getColumnIndex("ingre")));
HowTo = (TextView) findViewById(R.id.HowtoDo);
HowTo.setText(cursor.getString(cursor.getColumnIndex("howto")));
Information = (TextView) findViewById(R.id.Information);
Information.setText(cursor.getString(cursor.getColumnIndex("info")));
} }}
問題:データベースをclose()できないようです。どこでも検索しました。答えが見つかりません。このコマンドを試すたびに:
public void close()
{
Cook_tab_mains_data.close();
}
このエラーが発生し、コンパイルされません。Cannot make a static reference to the non-static method close() from the type SQLiteOpenHelper
んで、どうする?データベースを閉じるにはどうすればよいですか?