2017-11-10 215 views
-1

我想在用戶離開視圖後保存用戶的ScrollView位置。SQLite UPDATE命令不更新數據庫

我這樣做是通過嘗試捕獲getScrollY在之內的位置並將其保存到數據庫中,然後在用戶返回時檢索它。

我可以成功地添加和檢索位置(Log.i("scrolly", Integer.toString(scrollY))按預期返回),但ScrollView不會跳到正確的位置。

StoryBodyActivity的部分:

public class StoryBodyActivity extends AppCompatActivity { 

    private TextView storyBodyTextView; 
    private ScrollView storyBodyScrollView; 
    public int storyID; 
    Parcelable state; 
    int scrollY; 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_story_body, menu); 
     return true; 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_story_body); 

     Bundle extras = getIntent().getExtras(); 

     String story = extras.getString("story"); 
     storyID = extras.getInt("story_id"); 
     Log.i("stories", Integer.toString(storyID)); 

     storyBodyTextView = (TextView) findViewById(R.id.story_body_text_view); 
     storyBodyScrollView = (ScrollView) findViewById(R.id.story_body_scroll_view); 

     DatabaseHelper db = new DatabaseHelper(this); 
     scrollY = db.getScrollPosition(storyID); 
     Log.i("scrolly", Integer.toString(scrollY)); 

     storyBodyScrollView.scrollTo(0, scrollY); 

     String storyBody = db.getStoryBody(storyID); 

     storyBodyTextView.setText(Html.fromHtml(storyBody)); 

     if(state != null) { 
      Log.d("pause", "trying to restore textview state.."); 
      storyBodyTextView.onRestoreInstanceState(state); 
     } 

     int scroll = storyBodyScrollView.getScrollY(); 
     Log.i("scroll", Integer.toString(scroll)); 

    } 

    @Override 
    public void onPause() { 

     scrollY = storyBodyScrollView.getScrollY(); 
     Log.i("scroll", Integer.toString(scrollY)); 
     Log.i("insert", Integer.toString(storyID)); 
     DatabaseHelper db = new DatabaseHelper(this); 
     db.setScrollPosition(scrollY, storyID); 
     super.onPause(); 
    } 

} 

我DatabaseHelper的部分:

public class DatabaseHelper extends SQLiteAssetHelper { 

    private static final int DATABASE_VERSION = 1; 

    private static final String DATABASE_NAME = "database9.db"; 
    private static final String BOOKS = "books"; 
    private static final String AUTHORS = "authors"; 

    public DatabaseHelper (Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     setForcedUpgrade(); 
    } 

    public int setScrollPosition(int scrollY, int storyID) { 

     String insertQuery = "UPDATE " + BOOKS + " SET scroll_position = '" + scrollY + "' WHERE id = '" + storyID + "'"; 
     Log.i("insert", insertQuery); 
     SQLiteDatabase db = this.getWritableDatabase(); 
     db.execSQL(insertQuery); 

     return 0; 

    } 

    public int getScrollPosition(int storyID) { 

     int scrollPosition = 0; 

     String selectQuery = "SELECT scroll_position FROM " + BOOKS + " WHERE id = '" + storyID + "'"; 

     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 

     // looping through all rows and adding to list 
     if (cursor.moveToFirst()) { 
      do { 
       scrollPosition = cursor.getInt(0); 
      } while (cursor.moveToNext()); 
     } 

     return scrollPosition; 

     } 
} 

UPDATE:

我的架構:

CREATE TABLE "books" (
`id` INTEGER NOT NULL, 
`title` TEXT, 
`author_id` INTEGER, 
`collection` TEXT, 
`body` TEXT, 
`scroll_position` INTEGER, 
PRIMARY KEY(`id`)) 
+0

那麼,我以前的評論回答你的問題。 –

回答

0

在您的INSERT命令中,您傳遞的是字符串作爲id,而它期望整數

String insertQuery = "UPDATE " + BOOKS + " SET scroll_position = '" + scrollY + "' WHERE id = '" + storyID + "'"; 

你看? storyID作爲字符串傳遞("' WHERE id = '" + storyID + "'")!
單引號將該id標記爲字符串。

如果您創建一個整數列,它將永遠不會匹配一個字符串。

CREATE TABLE "books" (
`id` INTEGER NOT NULL, 
`title` TEXT, 
`author_id` INTEGER, 
`collection` TEXT, 
`body` TEXT, 
`scroll_position` INTEGER, 
PRIMARY KEY(`id`)) 

id字段被創建爲一個整數(這很好)。
但是你不能在你的命令中傳遞一個字符串(在你的查詢中)。

編輯

您還傳遞scroll_position領域。
這是一個整數。 而這也是行不通的。

+0

謝謝 - 我刪除了單引號,但我遇到同樣的問題 – Sebastian

+0

因此,沒有與傳入的ID匹配。確保傳入的ID存在於表中。請注意,ListView選擇的索引不一定匹配您的表格行ID。 –

+0

雖然我的代碼成功記錄了'scrollY'的輸出,但是當我在數據庫瀏覽器中查看錶時,沒有'scroll_position'的值。 – Sebastian