ユーザーのScrollViewの位置を保存した後に保存したい場合があります。SQLiteのUPDATEコマンドでデータベースが更新されない
私はこれをの中にgetScrollY
の位置にキャプチャしてデータベースに保存してから、ユーザーが戻ったときに取得しようとしています。
私は正常に位置を追加して取得できますが(Log.i("scrolly", Integer.toString(scrollY))
は期待通りに戻ります)、ScrollViewは正しい位置にジャンプしていません。 StoryBodyActivityの
パート:私のDatabaseHelperの
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();
}
}
パート:
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`))
私の前のコメントはあなたの質問に答えます。 –