我有一个铃声列表视图,在我的主片段中的每一行都有一个书签图标。我还有另一个片段来显示里面收藏的铃声。(当点击主片段中的书签图标时,特定的行数据将被保存到共享的偏好中并添加到喜欢的片段中。)
现在我的问题是当我点击一个项目使其成为收藏时它不会立即显示在我最喜欢的片段中。我试过这一切:
listView.requestLayout();
listView.refreshDrawableState();
listView.invalidate();
listView.invalidateViews();
adapter.notifyDataSetChanged();
它们都不起作用。或者我只需要分离并附加最喜欢的片段或关闭应用程序并重新打开它。
请帮帮我吗?
这就是我在主要片段
中添加项目到我的共享首选项的方法@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;
}
我从收藏片段中的共享偏好中获取我最喜欢的列表,如下所示:
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;
}
}
答案 0 :(得分:1)
将数据保存到共享首选项时,您希望更新列表。这可以使用共享首选项更改侦听器来完成。 在主Fragment中实现SharedPreferences.OnSharedPreferenceChangeListener并覆盖onSharedPreferenceChanged()方法。确保在oncreate()方法中注册共享首选项更改侦听器, 即sharedPreferences.registerOnSharedPreferenceChangeListener(this); 并在onDestroy()中取消注册。
因此,当您将数据添加到共享首选项时,您将在onSharedPreferenceChanged()中获得回调。因此,请在此方法中添加逻辑以刷新列表。
答案 1 :(得分:0)
您正在为ListView适配器分配一个列表
adapter = new CustomLightAdapter(activity, soundList) //soundList should be a global variable;
进行更改时,还应更新soundList。然后调用adapter.notifyDataSetChanged()
将使该更改反映在ListView