我想從一個擴展SQLiteOpenHelper的類調用一個方法(HomeFragment中的createButton)。我使用getSupportFragmentManager來打開這個片段,所以我需要傳遞上下文來使用這個getSupportFragmentManager而不會出錯。我已經以這種方式使用方面的嘗試:無法解析SQLiteOpenHelper onAttach()方法
private FragmentActivity myContext;
@Override
public void onAttach(Context context) {
myContext=(FragmentActivity) context;
super.onAttach(context);
}
public void buttonCreator(Drawable d,String a) {
Log.d("tag_name", "buttonCreator" + myContext);
HomeFragment homeFragment = (HomeFragment)myContext.getSupportFragmentManager().getFragments().get(1);
homeFragment.createButton(d, a);
}
但調用「super.onAttach(上下文)」當我得到一個錯誤,該方法onAttach()不能得到解決。 有沒有一種方法可以在SQLiteOpenHelper中使用onAttach(),以便我可以訪問getSupportFragmentManager以打開我的片段?
我自己也嘗試訪問背景是這樣的: 但是當我這樣做是「無法解析法‘getSupportFragmentManager()’」
private Context mContext;
public DatabaseStructure(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.d("tag_name", "Entered DatabaseStructure" + context);
mContext = context;
}
public void buttonCreator(Drawable d,String a) {
Log.d("tag_name", "buttonCreator" + myContext);
HomeFragment homeFragment = (HomeFragment)mContext.getSupportFragmentManager().getFragments().get(1);
homeFragment.createButton(d, a);
}
以下是完整的類代碼:
public class DatabaseStructure extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "database";
// Table Names
private static final String DB_TABLE = "table_image";
// column names
private static final String KEY_NAME = "image_name";
private static final String KEY_IMAGE = "image_data";
// Table create statement
private static final String CREATE_TABLE_IMAGE = "CREATE TABLE " + DB_TABLE + "("+
KEY_NAME + " TEXT," +
KEY_IMAGE + " BLOB);";
private Context mContext;
public DatabaseStructure(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.d("tag_name", "Entered DatabaseStructure" + context);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
// creating table
db.execSQL(CREATE_TABLE_IMAGE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// on upgrade drop older tables
db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
// create new table
onCreate(db);
}
public void addEntry(String name, byte[] image) throws SQLiteException {
SQLiteDatabase db = this.getWritableDatabase();
Log.d("tag_name", "Enter Add Entry" + db);
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_IMAGE, image);
db.insert(DB_TABLE, null, cv);
}
public boolean checkDatabase() {
Log.d("tag_name", "In CheckDatabase method");
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCursor = db.rawQuery("SELECT * FROM " + DB_TABLE, null);
if (mCursor.moveToFirst())
{
mCursor.moveToFirst();
String appName = mCursor.getString(0);
byte[] image = mCursor.getBlob(1);
Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
Drawable d = new BitmapDrawable(Resources.getSystem(), bitmap);
//Drawable d = new BitmapDrawable(bitmap);
buttonCreator(d, appName);
return true;
} else
{
return false;
}
}
private FragmentActivity myContext;
@Override
public void onAttach(Context context) {
myContext=(FragmentActivity) context;
super.onAttach(context);
}
public void buttonCreator(Drawable d,String a) {
Log.d("tag_name", "buttonCreator" + myContext);
HomeFragment homeFragment = (HomeFragment)myContext.getSupportFragmentManager().getFragments().get(1);
homeFragment.createButton(d, a);
}
}
我完全不懂這段代碼如何與'SQLiteOpenHelper'相關。你可以發佈完整的課程嗎?只要刪除這個主題不需要的方法。 –
爲什麼你想做任何與一個只負責管理數據庫的類相關的視圖?通常情況是這樣。 –
@ghostman我發佈了完整的Activity,它擴展了SQLiteOpenHelper。 – Natalie