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" />