【发布时间】: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