안드로이드 프로그램과 sqlite 데이터베이스에 문제가 있습니다. 스크롤 할 수있는 목록이있는 위젯을 만들고 있습니다. 따라서 "다음"버튼을 클릭하면 데이터베이스의 다음 행이 표시됩니다.안드로이드 SQLite 삭제 후 rowids을 변경
데이터베이스에서 항목을 삭제하고 행 ID가 누락 될 때까지 제대로 작동합니다. 나는 AUTOINCREMENT까지 ROWID를 설정해 놓았지만, 일부 데이터가 삭제 된 후에 나는 누락 된 숫자를 가지고 있으며, 그 숫자를 스크롤하려고하면 오류가 반환된다.
행이 삭제 된 후 순차 순서로 ROWID를 변경하는 다른 방법이 있는지 알고 싶었습니다. 또는 rowid를 참조하지 않고 각 행을 스크롤하는 더 좋은 방법이 있습니까?
편집 :이 내가 원유를 보인다 내 "다음"활동
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
c = this;
//get the shared data
myShared = getSharedPreferences(FILENAME, 0);
//gets NUM_LOC data from myShared or if that fails loads in number 1
myNum = myShared.getInt(NUM_LOC, 0);
//check to see if myNum is equal to or bigger than the total number
//and if it is, then set it back to one
FlashDataBase testDB = new FlashDataBase(this);
testDB.open();
int testNum = testDB.getNumber();
testDB.close();
if(myNum >= testNum){
myNum = 1;
}
else{
//set to next value
myNum ++;
}
//set the appwidgetmanager to this context
awm = AppWidgetManager.getInstance(c);
//make a remoteviews for the widget
RemoteViews v = new RemoteViews(c.getPackageName(), R.layout.widget);
ComponentName thisWidget = new ComponentName(c, VanillaProvider.class);
//get word from database
FlashDataBase fdb = new FlashDataBase(this);
fdb.open();
String someWord = fdb.getWord(myNum);
fdb.close();
//store current myNum into sharedpref
SharedPreferences.Editor editor = myShared.edit();
editor.putInt(NUM_LOC, myNum);
editor.commit();
//change the widget's remoteviews
//and update the appwidgemanager
v.setTextViewText(R.id.middle_button, someWord);
awm.updateAppWidget(thisWidget, v);
finish();
}
에 대해 가지고있는 코드입니다,하지만 난 생각하고 cursor.moveToNext()를 사용하는 방법이있다; 내 데이터베이스 클래스에 다음 번호를 얻으려면?
내 데이터베이스 클래스는 BTW 개인 정적 클래스 DbHelper 모든 도움을 {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// execute some SQL code
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
//+ KEY_NUM + " INTEGER NOT NULL, "
+ KEY_WORD + " TEXT NOT NULL, "
+ KEY_MEANING + " TEXT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public FlashDataBase(Context c) {
myContext = c;
}
// open our db
public FlashDataBase open() throws SQLException {
myHelper = new DbHelper(myContext);
myDatabase = myHelper.getWritableDatabase();
return this;
}
// and this closes it
public void close() {
myHelper.close();
}
public long createMethod(String word, String meaning) {
ContentValues cv = new ContentValues();
//cv.put(KEY_NUM, num);
cv.put(KEY_WORD, word);
cv.put(KEY_MEANING, meaning);
// and put these strings into our database
// inserts the cv into the table
return myDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
String[] columns = new String[] { KEY_ROWID, KEY_WORD, KEY_MEANING };
// set a cursor to read the info
Cursor c = myDatabase.query(DATABASE_TABLE, columns, null, null, null,
null, null);
String result = "";
// these ints are the same as getWord's method
// in other words, 0 1 and 2 respectively
int iWord = c.getColumnIndex(KEY_WORD);
int iMeaning = c.getColumnIndex(KEY_MEANING);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result += c.getInt(0) + " " + c.getString(iWord) + " " + c.getString(iMeaning) + "\n";
}
return result;
}
public String getWord(long l) {
String[] columns = new String[] { KEY_ROWID, KEY_WORD, KEY_MEANING };
Cursor c = myDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "="
+ l, null, null, null, null);
if (c != null) {
// since we set KEY_ROWID to itself plus Long (l), then the
// moveToFirst starts at L (l) ^as seen above
c.moveToFirst();
// thats a number 1 not letter l below
// which is the second column, aka int iWord from the getData class
String word = c.getString(1);
return word;
}
return null;
}
public String getMeaning(long l) {
String[] columns = new String[] { KEY_ROWID, KEY_WORD, KEY_MEANING };
Cursor c = myDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "="
+ l, null, null, null, null);
if (c != null) {
// since we set KEY_ROWID to itself plus Long (l), then the
// moveToFirst starts at L (l) ^as seen above
c.moveToFirst();
// thats a number 2 below
// which is the second column, aka int iWord from the getData class
String meaning = c.getString(2);
return meaning;
}
return null;
}
public int getNumber() {
String[] columns = new String[] { KEY_ROWID, KEY_WORD, KEY_MEANING };
// set a cursor to read the info
Cursor c = myDatabase.query(DATABASE_TABLE, columns, null, null, null,
null, null);
int result = 0;
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result++;
}
return result;
}
감사 SQLiteOpenHelper 확장이 제품은 또한
* 최종 편집 : 나는 다른 무엇보다 조금 그것을 알아 냈다 나에게 말했다. 그래서 그 대신 공유 정수의) 행의 총량보다 더 많은 경우 재설정 할 공유 정수 값의 수를 유지하고 c.moveToNext를 (사용하여 데이터베이스 클래스 자체에 루프를했다 :
public String getWord(long l) {
String[] columns = new String[] { KEY_ROWID, KEY_WORD, KEY_MEANING };
Cursor c = myDatabase.query(DATABASE_TABLE, columns, KEY_ROWID, null, null, null, null);
String result;
c.moveToFirst();
for(int i=1; i<=l; i++){
if(c.isLast()){
c.moveToFirst();
result = c.getString(1);
return result;
}
c.moveToNext();
}
result = c.getString(1);
return result;
}
ID를 항상 늘리면 (공백을 채우지 않아도 됨) 생각하면됩니다. 이 코드는 전혀 문제가 아니어야하므로 코드를 게시해야합니다. –