【问题标题】:Listview will not refresh after retrieving data from shared preferences从共享首选项中检索数据后,Listview 不会刷新
【发布时间】:2017-09-23 06:10:12
【问题描述】:

我的主片段内的每一行都有一个带有书签图标的铃声列表视图。我还有另一个 Fragment 可以在其中显示收藏的铃声。(当我的主 Fragment 中的书签图标被点击时,该特定行数据将保存到共享首选项中并添加到收藏的 Fragment 中)。

现在我的问题是,当我单击一个项目将其设为收藏时,它不会立即显示在我最喜欢的片段中。我尝试了所有这些:

listView.requestLayout();
listView.refreshDrawableState();
listView.invalidate();
listView.invalidateViews();
adapter.notifyDataSetChanged();

它们都不起作用。或者我只需要分离并附加最喜欢的 Fragment 或关闭应用程序并重新打开它。

你能帮帮我吗?

这就是我如何在 Main Fragment

中将项目添加到我的共享首选项中
@Override
public boolean favOnClick(int position , View v) {
    ProductLight product = soundList.get(position);
    ImageView button = (ImageView) v.findViewById(R.id.favImageHive);
    String tag = button.getTag().toString();
    if (tag.equalsIgnoreCase("grey")) {
        sharedPreference.addFavorite(product);
        snackS("Added to Favorites");
        button.setTag("red");
        button.setImageResource(R.mipmap.bookmarked);
    } else {
        sharedPreference.removeFavorite(product);
        button.setTag("grey");
        button.setImageResource(R.mipmap.bookmark_border);
        snackS("Removed from Favorites");
    }
    return true;
}

我从 Favorite Fragment 中的共享偏好中获得我最喜欢的列表,如下所示:

private List<ProductLight> soundList = new ArrayList<ProductLight>();
.
.
.

soundList = sharedPreference.getFavorites();
adapter = new CustomLightAdapter(activity, soundList);
listView.setAdapter(adapter);

共享偏好

public class SharedPreference_light {

public static final String PREFS_NAME = "Light_Products";
public static final String FAVORITES = "Favorite_Tones_Light";

SharedPreferences settings;
SharedPreferences.Editor editor;
Gson gson = new Gson();

public SharedPreference_light(Context context) {
    settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    editor = settings.edit();
}

// This four methods are used for maintaining favorites.
public void saveFavorites(List<ProductLight> favorites) {
    String jsonFavorites = gson.toJson(favorites);
    editor.putString(FAVORITES, jsonFavorites);
    editor.apply();
}

public void addFavorite(ProductLight product) {
    List <ProductLight> favorites = getFavorites();
    if (favorites == null)
        favorites = new ArrayList<ProductLight>();
    favorites.add(product);
    saveFavorites(favorites);
    Log.w("addPrefLog", favorites.toString());
}

public void removeFavorite(ProductLight product) {
    ArrayList <ProductLight> favorites = getFavorites();
    if (favorites != null) {
        favorites.remove(product);
        saveFavorites(favorites);
    }
}

public ArrayList <ProductLight> getFavorites() {
    List<ProductLight> favorites;
    if (settings.contains(FAVORITES)) {
        String jsonFavorites = settings.getString(FAVORITES, null);
        ProductLight[] favoriteItems = gson.fromJson(jsonFavorites, ProductLight[].class);
        favorites = Arrays.asList(favoriteItems);
        favorites = new ArrayList <ProductLight> (favorites);
    } else
        return null;

    return (ArrayList <ProductLight> ) favorites;
  }
}

【问题讨论】:

  • 点击收藏按钮时从列表片段中调用收藏片段的方法,这将更新收藏项目列表
  • editor.apply() 是一个异步函数,也许你可以在 onSharedPreferenceChanged 中调用一个接口监听器。还是使用 editor.commit() 代替??
  • @FarukItsDO 是的,但是如果我保存太多项目,commit() 会减慢我的列表视图滚动速度。顺便试了一下,还是不行。

标签: android listview android-fragments fragment sharedpreferences


【解决方案1】:

当您将数据保存到共享首选项中时,您希望更新列表。这可以使用共享首选项更改侦听器来完成。 在主 Fragment 中实现 SharedPreferences.OnSharedPreferenceChangeListener 并覆盖 onSharedPreferenceChanged() 方法。确保在 oncreate() 方法中注册共享首选项更改侦听器, 即 sharedPreferences.registerOnSharedPreferenceChangeListener(this); 并在 onDestroy() 中注销。

因此,当您将数据添加到共享首选项中时,您将在 onSharedPreferenceChanged() 中获得回调。因此,在此方法中添加您的逻辑以刷新您的列表。

【讨论】:

  • 我认为我的共享偏好运行良好。因为(考虑我在最喜欢的片段中有一个项目并显示在该列表视图中)现在在我的主片段中,如果我单击取消标记该项目,最喜欢的片段中的书签图标将变为未选中,但项目本身仍然存在。跨度>
【解决方案2】:

您正在为 ListView 适配器分配一个列表

adapter = new CustomLightAdapter(activity, soundList) //soundList should be a global variable;

进行更改时,soundList 也应该更新。然后调用 adapter.notifyDataSetChanged() 将使更改反映在 ListView 中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-29
    • 2016-01-07
    • 2019-03-02
    • 1970-01-01
    • 2020-10-04
    • 2020-08-18
    • 1970-01-01
    相关资源
    最近更新 更多