我的 Android 应用程序已成功写入 SQLlite 数据库,但写入的 _id 位置为 0。无论如何我可以将初始写入代码调整到数据库,而不是从位置/行 0 开始?我正在使用这种测试方法从微调器/下拉列表中验证当前选定人员的位置,以最终删除客户端。
我能够像这样测试它的位置:
Cursor c = db.getClient(currentlySelected+1);
返回位置 1 的成功测试 toast 消息,我这样做是为了验证第一个位置是否为 0。Cursor c = db.getClient(currentlySelected);
崩溃并且 DDMS 报告 CursorIndexOutOfBounds 错误,请求 0 和 0 或类似的东西。
DB 是一个数据库适配器:
db.open();
long currentlySelected = contactList.getSelectedItemId(); //spinner populated from database
Cursor c = db.getClient(currentlySelected+1);
Toast.makeText(LookupReturn.this,
"Cursor position: " + c.getLong(c.getColumnIndex("_id")),
Toast.LENGTH_LONG).show();
//db.deleteClient(currentlySelected);
c.close();
db.close();
-------------------------- 下面的部分是我的解决方案 ------------------ ----------
db.open();
long currentlySelected = contactList.getSelectedItemId()+1;
long currentlySelectedTemp = currentlySelected; //holds initial position value when this value is removed from the main db
Cursor c = db.getClient(currentlySelected);
if(c.moveToFirst())
{
Toast.makeText(LookupReturn.this, "Position: " + currentlySelected, //c.getLong(c.getColumnIndex("_id")),
Toast.LENGTH_LONG).show();
db.deleteClient(currentlySelected);
c.close();
db.close();
Toast.makeText(LookupReturn.this, "Deleted",
Toast.LENGTH_LONG).show();
deleteClient.setChecked(false);
}
//remove from secondary db
DatabaseHandler dbh = new DatabaseHandler(getApplicationContext());
SQLiteDatabase s = dbh.getWritableDatabase();
s.execSQL("DELETE FROM returnvisits WHERE _id = " + currentlySelectedTemp);
s.close();