1

我正在尝试获取数据库列表。它正在创建数据库,但我认为我在检索数据时遇到了问题。请帮我。主要活动。在模拟器中显示应用程序意外停止

MainActivity.java

公共类 MainActivity 扩展 ListActivity {

private DbAdapter Database;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView listContent = (ListView) findViewById(R.id.list);

    Database= new DbAdapter(this);
    Database.open();

    Cursor c = Database.GetUnits();
    startManagingCursor(c);

    String[] from = new String[]{Database.UNITS};
    int[] to = new int[]{R.id.list};

    SimpleCursorAdapter notes = new SimpleCursorAdapter(this,R.layout.activity_main,c,from,to);
    setListAdapter(notes);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

` DbAdapter.java

公共类 DbAdapter {

public static final String DATABASE_NAME="bible";
public static final String DATABASE_TABLE="test";
public static final int DATABASE_VERSION=1;
public static final String KEY_ID="_id";
public static final String UNITS="units";
public static final String CHAPTERS="chapters";

private static final String CREATE_DATABASE="create table test (_id integer primary key autoincrement, "
        +"units text not null, chapters text not null)";

private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;

private Context context;

//constructor

public DbAdapter(Context c){
    context = c;
}

private static class SQLiteHelper extends SQLiteOpenHelper{

    public SQLiteHelper(Context context,String name,CursorFactory factory,int version){

        super(context,name,factory,version);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(CREATE_DATABASE);
        ContentValues insert = new ContentValues();
        insert.put(UNITS, "Unit1");
        insert.put(CHAPTERS, "CHAPTER1");
        db.insert(DATABASE_TABLE,null,insert);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

}

public DbAdapter open() throws SQLException{
    sqLiteHelper = new SQLiteHelper(context,DATABASE_NAME,null,DATABASE_VERSION);
    sqLiteDatabase = sqLiteHelper.getWritableDatabase();
    return this;
}

public void close(){
    sqLiteHelper.close();
}

public Cursor GetUnits()
{
    Cursor mCursor=sqLiteDatabase.query(DATABASE_TABLE, new String[]{ UNITS}, null, null, null, null, null);
    if(mCursor!=null){
        mCursor.moveToFirst();
    }
    return mCursor;
}

}

4

0 回答 0