我查看了上下文信息,它在活动组的已知间接子类中说,“这个类已被弃用。改用新的 Fragment 和 FragmentManager API”那么为什么在下面的代码中它不能在我扩展 ListFragment 的类中工作?当我在我的类中尝试扩展 Activity 虽然它可以工作时(它应该在已知的间接子类中)。我需要做什么才能打开数据库并在 AFragmentTab 中获取信息?提前谢谢您,如果可能,请提供邮政编码。
数据库助手类:
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.
}
}