【问题标题】:Persist checked status of items in ListView保持 ListView 中项目的检查状态
【发布时间】:2018-10-22 18:30:45
【问题描述】:

我有一个ListViewCheckedTextView 项目,每个项目都使用一个单选按钮。这是一个单选列表,表示我的主要活动中正在播放的音频流。

一旦其中一个项目被选中,它应该保持选中状态,直到选中另一个项目,或者直到用户“停止”来自通知或 UI 的流,并且每次应用程序退出时都需要重置选中状态.

此刻我正在这样做:

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
    }
});

在我的其他活动打开之前,当您选择列表项时,您可以看到该项目正在被选中。但是,当我返回主要活动时,不再检查任何项目。

在应用程序关闭或我手动取消选中之前,保持项目的选中状态的正确方法是什么?

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

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

【问题讨论】:

    标签: java android listview android-activity android-checkedtextview


    【解决方案1】:

    我会在列表中的所有对象中再添加一个字段。它可以被布尔检查,默认为假,当它被点击时使其为真。然后你可以很容易地保存对象的状态。如果您更改状态,只需通知适配器它应该重绘自己。

    【讨论】:

      【解决方案2】:

      您可以将选中的项目存储在 SharedPreferences 中。 SharedPreferences 是由应用程序上下文管理的配置文件中的简单存储。最好的是,即使应用程序被关闭或杀死,它仍然是持久的。

      每个应用程序都有自己的 SharedPreferences 文件存储在其上下文文件目录中(存储在磁盘中)。

      要存储持久值,您可以编写以下代码:

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

      然后你可以通过写来检索存储的选中项位置:

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

      0,如果没有存储key(checked_item)则返回默认值。

      之后,您可以在每次创建列表活动时手动将该项目标记为选中。

      您可以将项目位置存储在 onItemClick() 中,您可以检索它并在活动中标记为已在 onCreate() 或 onResume() 中选中。

      有关更多信息,您可以阅读 SharedPreferences 和 PreferenceManager 文档。

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

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

      希望这会有所帮助。

      【讨论】:

      • 谢谢,我认为这比将isChecked 添加到我的模型本身更可取,因为它需要存储在数据库等中,而仅在一项活动中只需要一次。我会试试看有没有问题
      猜你喜欢
      • 2011-08-06
      • 1970-01-01
      • 1970-01-01
      • 2011-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-09
      • 1970-01-01
      相关资源
      最近更新 更多