0

having thoroughly serched before, I could not find a question that could help me.

My issue is having a ListView, which automatically gets populated from a SQL-database upon opening the app. Now, the user can make a selection for one item in this list. The selection is treated with public void onItemClick() In order to increase the usability, I'd like to have this selection remembered after closing and re-opening the app.

I've tried everything.. Hope you can help me with an idea.

Jules
  • 1
  • 1
  • so what have you tried? – pskink Jul 22 '15 at 18:12
  • I think that you're looking for SharedPreferences http://stackoverflow.com/questions/29284705/android-saving-data-upon-closing-app-and-retrieving-that-data – StephenG Jul 22 '15 at 18:14
  • I've tried to implement some sort of 'selector column' in my SQL-db but this seems to be a little too complicated. So SharedPreferences was the next thing... I wonder how to implement a syntax which saves a selection state.. – Jules Jul 22 '15 at 18:17
  • @Jules you just need to save the id of the selected column. It's the `id` parameter from the `onListItemClick` method. – LordRaydenMK Jul 22 '15 at 18:19
  • yes, SharedPreferences is a way to follow, just save an ID, i assume you are using CursorAdapter – pskink Jul 22 '15 at 18:23
  • @LordRaydenMK This would be fine but what happens if elements are added to the list, there id according to the elements would change as well, wouldn't they? – Jules Jul 22 '15 at 18:24
  • @pskink yeah I create a 'CursorAdapter' and then use it to populate the 'ListView' – Jules Jul 22 '15 at 18:25
  • so its ok CursorAdapter has stable ids – pskink Jul 22 '15 at 18:26
  • @Jules the `id` is the column `_id` from the SQLite database. It's the primary key. Adding items to the database doesn't affect the id's of the previous elements. – LordRaydenMK Jul 22 '15 at 18:27
  • @LordRaydenMK @pskink so you mean the `id` returned by `onListItemClick` is the PrimaryKey used by the SQL db...? Then it would be easy to put into a SharedPreferences thing – Jules Jul 22 '15 at 18:33
  • @Jules if you are using a `CursorAdapter` the answer is YES!! – LordRaydenMK Jul 22 '15 at 18:34
  • @LordRaydenMK thank you very much! I'll go for that and notify you if it was succesful or not. – Jules Jul 22 '15 at 18:38
  • yes if you are using `CursorAdapter`, 90% of folks here use `ArrayAdapter` but i think you are smart enough to use `CursorAdapter` to make things simple – pskink Jul 22 '15 at 18:38
  • @pskink yes I always use it when working on SQL DBs. However do you know how to set the item with the saved `id` selected on re-opening the app? Beacause the method `listView.setSelection()` requires the position NOT the id of the View. – Jules Jul 22 '15 at 18:51
  • so you need to find the position for which adapter.getItemId(position) == savedId – pskink Jul 22 '15 at 19:16
  • @pskink `myList.setSelection(position)` unfortunately does not work out. According to the API it has no effect _when in touch-mode_ Would you have any idea to set a selection? – Jules Jul 24 '15 at 06:55
  • `public abstract void setSelection (int position) Sets the currently selected item. To support accessibility subclasses that override this method must invoke the overriden super method first.` i can't see any notes about touch-mode – pskink Jul 24 '15 at 06:59
  • "public void setSelection (int position) Added in API level 1 Sets the currently selected item. If in touch mode, the item will not be selected but it will still be positioned appropriately. If the specified selection position is less than 0, then the item at position 0 will be selected." [link](http://developer.android.com/reference/android/widget/ListView.html#setSelection(int)) – Jules Jul 24 '15 at 07:05
  • "but it will still be positioned appropriately" isn't it what you are looking for? if you want that item to be shown in a different way use `ViewBinder` – pskink Jul 24 '15 at 07:32

1 Answers1

0

create an extra column in your data base called checked,

(CREATE TABLE ..... checked int...)

when the user checks your listItem in

onListItemClick
{
    SqliteDatabase db = getDb();
    //set all to unchecked
    db.exec("UPDATE ..table.. set checked = 0");
    //set your choice to checked
    db.exec(UPDATE ...table... set checked=1 WHERE id = "+cellid+");
}

then on start up in your list adapter you get the value of checked out of the database and update the UI accordingly.

I have used this pattern in several apps and it does work well.

Mark Gilchrist
  • 1,972
  • 3
  • 24
  • 44