2017-11-10 218 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