我通過viewpager有兩個不同視圖(片段)的swipeview。我現在希望這兩個頁面顯示來自sqllite數據庫的不同數據。問題是我無法弄清楚如何做到這一點,從主要活動我無法訪問我的片段和片段源代碼視圖(TableLayout)我不能訪問數據庫,因爲適配器不會打開(適配器的上下文是super.getActivity())。有沒有辦法做到這一點?在SwipeView中顯示數據庫的Android
代碼片段的:
DBAdapter adapter = new DBAdapter(getActivity());
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
if(container==null){
return null;
}
layout=(RelativeLayout)inflater.inflate(R.layout.layout_morning, container, false);
adapter.open();
adapter.close();
代碼將對DBAdapter的:
private static String DB_NAME = "database.dat";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DB_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public DBAdapter open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
public void close() {
if (db!=null){
db.close();
}
DBHelper.close();
}
錯誤是一個NullPointerException
嘗試初始化在onCreateView方法你將對DBAdapter ... – yahya 2013-02-16 22:30:39