2

コンテキスト情報を調べたところ、Activity Group の既知の間接サブクラスに「このクラスは非推奨です。代わりに新しい Fragment および FragmentManager API を使用してください」と表示されています。Activity を拡張するクラスで試してみたところ、それは機能します (Known Indirect Subclasses に含まれているため)。データベースを開いて 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 Class:

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.   
}
}
4

1 に答える 1

4

Fragment広がらずActivity、間接的Contextに。したがって、コードを次のように変更します。

myDB = new DataBaseHelper(getActivity());

メソッドgetActivity()は、フラグメントがアタッチされているアクティビティの参照を返すため、コンテキストを渡すためのソリューションを提供します。

于 2012-09-01T16:27:12.237 に答える