0

I have a ListView of CheckedTextView items, each one uses a radio button. It is a single-choice list indicating a now-playing audio stream in my main activity.

Once one of the items is checked it should remain checked until another item is checked, or until the users 'stops' the stream from a notification or the UI, and the checked status needs to be reset every time the application is quit.

At the minute I'm doing this:

final BookmarkAdapter adapter = new BookmarkAdapter(this, db.getAllBookmarks());
final ListView listView = findViewById(R.id.bookmark_list_view);

listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        View v = listView.getChildAt(position);
        CheckedTextView ctv = v.findViewById(R.id.bookmark_list_item);
        ctv.toggle();
        // ... go on to launch a service and another activity
    }
});

You can see that the item is being checked when you select the list item, before my other activity opens. However, when I return to the main activity, no item is checked any more.

What is the correct way to persist the checked state of the items until the application is closed or I uncheck it manually?

The ListView xml:

<ListView
        android:id="@+id/bookmark_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

The CheckedTextView xml:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/bookmark_list_item"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:checkMark="?android:attr/listChoiceIndicatorSingle"
        android:gravity="center_vertical"
        android:paddingLeft="16dip"
        android:paddingRight="16dip"
        android:text="@string/list_item_title"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
transiti0nary
  • 493
  • 6
  • 25

2 Answers2

1

You could store the checked item in SharedPreferences. SharedPreferences is a simple storage in a configuration file managed by the context of your application. And the best is that it's persistent even if the app is closed or killed.

Each application has its own SharedPreferences file stored in it's context files directory (stored in disk).

To store persistent value you can write the following:

SharedPreferences prefs =  PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putInt("checked_item", position).apply();

Then you can retrieve the stored checked item position by writing:

int checkedPosition = prefs.gerInt("checked_item", 0);

0, is the default value returned if there is no key (checked_item) stored.

Afterwards you can mark as checked manually the item every time the list activity is created.

You could store the item position in onItemClick() and you could retrieve it and mark as checked in onCreate() or onResume() in your activity.

For more info you can read the SharedPreferences and PreferenceManager docs.

SharedPreferences https://developer.android.com/reference/android/content/SharedPreferences

PreferenceManager https://developer.android.com/reference/android/preference/PreferenceManager

Hope this helps.

Marc Estrada
  • 1,657
  • 10
  • 20
  • 1
    thanks, I think this is preferable to adding `isChecked` to my model itself as then it would need to be stored in database etc when it is only ever needed this one time in one activity. I will try this out and see if there's any issues – transiti0nary May 15 '18 at 15:40
1

I would have one more field in all Objects that are in list. It could be boolean checked, default false, when it is clicked make it true. Then you could save state of object easy. If you change state, just notify adapter that it should redraw itself.