DataBaseHelper没有获取ListFragment的上下文

时间:2012-09-01 16:24:31

标签: android database sqlite

我查看了Context信息,并在活动组的已知间接子类中说,“这个类已被弃用。请改用新的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.   
}
}

1 个答案:

答案 0 :(得分:4)

Fragment不会延伸Activity和间接Context。所以改变你的代码如下:

myDB = new DataBaseHelper(getActivity());

方法 getActivity()返回您的片段附加到的活动的引用,从而提供传递上下文的解决方案。