android - 在另一个 Activity 中访问在一个 Activity 中创建的数据库

标签 android sqlite null instance

出于某种奇怪的原因,我似乎无法从一个 Activity 中创建的数据库中提取信息到另一个 Activity 中。我对静态数组列表也有同样的问题。认为使用数据库会容易得多,但它一直在崩溃。我的数据库实例在下一个 Activity 中始终为空,即使我在提取任何信息之前调用它也是如此。这是我在 fragment 类中的函数

private void populateListView(){
    Cursor cursor = myDatabase.getAllData();
    String[] fromfieldNames = new String[]{StudentDBOpenHelper.KEY_ID,StudentDBOpenHelper.ITEM_NAME_COLUMN};
    int[] toViewIDs = new int[] {R.id.textView_itemNumber,R.id.textView_itemName};
    SimpleCursorAdapter myCursorAdapter;
    myCursorAdapter = new SimpleCursorAdapter(getActivity().getBaseContext(),
            R.layout.individualview,cursor,fromfieldNames,toViewIDs,0);
    ListView myList = (ListView) getActivity().findViewById(R.id.courseListXML);
    myList.setAdapter(myCursorAdapter);
}//USed to populate list view

该函数本身位于onCreateView 和onViewCreated 之外,但在onViewCreated 中调用。

这是我的数据库助手类

public class StudentDBOpenHelper extends SQLiteOpenHelper {

public static final String KEY_ID = "_id";

public static final String ITEM_NAME_COLUMN = "ITEM_NAME_COLUMN";

public static final String ITEM_CATEGORY_COLUMN = "ITEM_CATEGORY_COLUMN";

public static final String ITEM_GRADE_COLUMN = "ITEM_GRADE_COLUMN";


public static final String DATABASE_NAME = "myItemDatabase.db";
public static final String DATABASE_TABLE = "ItemInfo";
public static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE ="create table " + DATABASE_TABLE + " (" +
        KEY_ID + " integer primary key autoincrement, " +
        ITEM_NAME_COLUMN + " text, " +
        ITEM_CATEGORY_COLUMN + " text, "
        +ITEM_GRADE_COLUMN + " integer);";




/*
public StudentDBOpenHelper(Context context,String name,
                           SQLiteDatabase.CursorFactory factory,int version)   {
 super(context,name,factory,version);



}
*/

public StudentDBOpenHelper(Context context){
    super(context, DATABASE_NAME, null, DATABASE_VERSION);


}


@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL((DATABASE_CREATE));
}

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

    Log.w("TaskDBAdapter", "Upgrading from version" +
            oldVersion + ",which will destroy all old data");

    db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE);
    onCreate(db);

}

public boolean insertData(String firstname,String lastname,String credits){

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues(); //instance of class
    contentValues.put(ITEM_NAME_COLUMN,firstname);//2 parameters(Column name in which you
    //you want to insert data
    // and second is the value itself
    contentValues.put(ITEM_CATEGORY_COLUMN, lastname);
    contentValues.put(ITEM_GRADE_COLUMN, credits);

    long result = db.insert(DATABASE_TABLE,null,contentValues);
    if(result == -1) {
        return false;
    }
    else {

        return true;
    }
}//ends insertData functin

public Cursor getAllData(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor result = db.rawQuery("select * from " + DATABASE_TABLE,null);
    return result;
}//ends cursor


public boolean updateData(String id,String firstname,String lastname,String credits){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(KEY_ID,id);
    contentValues.put(ITEM_NAME_COLUMN,firstname);//2 parameters(Column name in which you
    //you want to insert data
    // and second is the value itself
    contentValues.put(ITEM_CATEGORY_COLUMN, lastname);
    contentValues.put(ITEM_GRADE_COLUMN, credits);

    db.update(DATABASE_TABLE,contentValues,"_id = ?",new String[]{id});

    return true;
}

public Integer deleteData(String id){
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(DATABASE_TABLE,"_id = ?",new String[] {id});
}
}

最佳答案

初始化数据库的正确方法如下:

StudentDBOpenHelper myDatabase= new StudentDBOpenHelper(this);

如果它是 Activity,则使用 this 作为参数,如果它是 Fragment,则使用 getContext()

关于android - 在另一个 Activity 中访问在一个 Activity 中创建的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37031503/

相关文章:

java - Android:DigitalClock 删除秒数

android - 将按钮缩放到屏幕大小

android - 如何用两个手指滚动位图?

android - Intent.ACTION_PICK 为某些联系人返回空光标

java - 从数组中返回不为空的值。 java

PHP:获取关联数组返回 null,尽管存在于 var_dump 中

android - 如何在 ListView 中设置 RatingBar 的样式? (API 21 和低版本中的问题)

php - 我如何通过php执行sqlite备份

c++ - sqlite3错误: no such table: when checking if table exists

java - JBox2D Body 曾经存在,现在为空?