我试图从扩展SQLiteOpenHelper 的类中调用一个方法(HomeFragment中的createButton)。我正在使用getSupportFragmentManager来打开这个片段,所以我需要传递上下文来使用这个getSupportFragmentManager而不会出错。我试过这样使用上下文:
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(context)”时遇到错误,无法解析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);
}
}
答案 0 :(得分:3)
onAttach()
是Fragment
的回调方法,您可以覆盖该方法,以便在您的片段类中附加侦听器或其他内容。
根据您的说法,您将课程从SQLiteOpenHelper
扩展,因此您没有任何onAttach()
方法可以覆盖。
<答案
有没有办法可以在
SQLiteOpenHelper
中使用onAttach()以便我可以 访问getSupportFragmentManager打开我的片段?
- 否强>
另外,关于如何获取上下文,我不知道为什么要将课程扩展到SQLiteOpenHelper
,但如果您想获取上下文来启动片段或使用getSupportFragmentManager()
,您可以在创建其对象时传递该类中的上下文。