gpt4 book ai didi

android - DataBaseHelper 没有获取 ListFragment 的上下文

转载 作者:搜寻专家 更新时间:2023-11-01 08:08:24 25 4
gpt4 key购买 nike

我查看了上下文信息,它在 Activity 组的已知间接子类中说,“此类已弃用。请改用新的 Fragment 和 FragmentManager API” 那么为什么下面的代码在我的类中不起作用扩展 ListFragment?当我在我的类中尝试扩展 Activity 虽然它有效时(它应该因为它在已知的间接子类中)。我需要做什么才能打开数据库并获取 AFragmentTab 中的信息?在此先感谢您,如果可能请提供邮政编码。

DataBaseHelper 类:

public class DataBaseHelper extends SQLiteOpenHelper{

private static String DB_PATH = "/data/data/com.example.alotoftesting/databases/";
private static String DB_NAME = "library1_dev.db";
private SQLiteDatabase myDataBase;
private final Context myContext;
Cursor cursor;


public DataBaseHelper(Context c){
super(c, DB_NAME, null, 1);
this.myContext = c;
}

public void createDataBase() throws IOException{

boolean dbExist = checkDataBase();

if(dbExist){
//do nothing since the database already exist.
System.out.println("Database Exist");
}
else{
System.out.println("2.5");
this.getReadableDatabase();

try{
System.out.println("3.");
copyDataBase();
System.out.println("We just copied the database.");
}catch(IOException e){
throw new Error("Error Copying Database");
}
}
}

private boolean checkDataBase(){

SQLiteDatabase checkDB = null;
System.out.println("1.");
try{
String myPath = DB_PATH + DB_NAME;
System.out.println("2.");
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
System.out.println("We are in here");
}catch(SQLiteException e){
//database doesn't exist yet.
}

if(checkDB != null){
checkDB.close();
System.out.println("Database Closed");

}

return checkDB != null ? true : false;
}

private void copyDataBase()throws IOException{

//Opens your local db as the input stream.
InputStream myInput = myContext.getAssets().open(DB_NAME);

//Path to the just created empty DB.
String outFileName = DB_PATH + DB_NAME;

//Opens the empty DB as the output stream.
OutputStream myOutput = new FileOutputStream(outFileName);

//transfer bytes from the inputfile to the outputfiles.
byte[] buffer = new byte[1024];
int length;
while((length = myInput.read(buffer)) > 0){
myOutput.write(buffer, 0, length);
}

//Close the Streams.
myOutput.flush();
myOutput.close();
myInput.close();
}

public void openDatabase() throws SQLException{

//Open the database.
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}

public void closeDatabase() throws SQLException{

//Close the database.
myDataBase.close();
}

public ArrayList<String> getTableColumn(String tableName, String[] column){

ArrayList<String> aL = new ArrayList<String>();

cursor = myDataBase.query(tableName, column, null, null, null, null, null);

cursor.moveToFirst();
while (!cursor.isAfterLast()) {
aL.add(cursor.getString(0));
cursor.moveToNext();
}

System.out.println(aL);
cursor.close();
return aL;
}

@Override
public void onCreate(SQLiteDatabase db){

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){

}


}

我想打开数据库的类,AFragmentTab类:

public class AFragmentTab extends ListFragment{

DataBaseHelper myDB;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);

myDB = new DataBaseHelper(this); //<------The error is here.
}
}

最佳答案

Fragment 不扩展 Activity 并间接扩展 Context。所以改变你的代码如下:

myDB = new DataBaseHelper(getActivity());

getActivity() 方法返回您的 fragment 所附加的 Activity 的引用,从而提供传递上下文的解决方案。

关于android - DataBaseHelper 没有获取 ListFragment 的上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12229434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com