gpt4 book ai didi

java - 将列表中最喜欢的项目存储到 android 中的 SharedPreferences 中?

转载 作者:行者123 更新时间:2023-12-01 17:16:40 27 4
gpt4 key购买 nike

我正在调用 Firebase firestore 中的项目列表。并将其设置为回收 View 。我已经实现了通过将项目的位置存储到共享首选项的数组列表中来将其保存为 Collection 夹的功能。但我面临的问题是:假设recyclerview中有5个项目。我喜欢其中的 3 件商品。我毁掉了我的应用程序。当我再次打开我的应用程序时,我又喜欢了一件元素,然后再次破坏了我的应用程序。之后,当我再次打开我的应用程序时,就像只有上次我喜欢的一样。前 3 件喜欢的元素消失了。我不明白为什么会发生。*注意:到目前为止,我还没有添加从项目列表中删除“喜欢”的功能。

下面是我的逻辑代码:

共享偏好模型

public class SharedPref {
SharedPreferences preferences;
SharedPreferences.Editor editor ;
public SharedPref(Context context) {
// TODO Auto-generated constructor stub
preferences =context.getSharedPreferences("Stackoverflow", Context.MODE_PRIVATE);
editor =preferences.edit();
}

public boolean putBoolean(String key, boolean value) {
editor.putBoolean(key, value);
return editor.commit();
}

public boolean getBoolean(String key) {
return preferences.getBoolean(key, false);
}
public boolean putString(String key, String value) {
editor.putString(key, value);
return editor.commit();
}
public String getString(String key) {
return preferences.getString(key, null);
}

public boolean putLong(String key, long value) {
editor.putLong(key, value);
return editor.commit();
}

public long getLong(String key) {
return preferences.getLong(key, 0);
}

public boolean putInt(String key, int value) {
editor.putInt(key, value);
return editor.commit();
}
public int getInt(String key) {
return preferences.getInt(key, 0);
}

public boolean putFloat(String key, float value) {
editor.putFloat(key, value);
return editor.commit();
}
public float getFloat(String key) {
return preferences.getFloat(key, 0);
}
public boolean remove(String key){
return editor.remove(key).commit();
}
public boolean clear() {
// TODO Auto-generated method stub
return editor.clear().commit();
}

public void putListString(String key, ArrayList<String> stringList) {
checkForNullKey(key);
String[] myStringList = stringList.toArray(new String[stringList.size()]);
preferences.edit().putString(key, TextUtils.join("‚‗‚", myStringList)).apply();
}

public void checkForNullKey(String key){
if (key == null){
throw new NullPointerException();
}
}

public ArrayList<String> getListString(String key) {
return new ArrayList<String>(Arrays.asList(TextUtils.split(preferences.getString(key, ""), "‚‗‚")));
}
}

适配器类:

Boolean favimageclick=true;
SharedPref sharedPref;
ArrayList<String> favlist=new ArrayList<>();

if (sharedPref.getListString("fav")!=null)
{
Log.e("StoredData","______"+sharedPref.getListString("fav"));
for(String data:sharedPref.getListString("fav"))
{
if (data.equalsIgnoreCase(String.valueOf(position))) {
holder.imageViewFav.setBackgroundResource(R.drawable.ic_favorite);
}
}
}

holder.imageViewFav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (favimageclick) {
holder.imageViewFav.setBackgroundResource(R.drawable.ic_favorite);
favlist.add(String.valueOf(position));
sharedPref.putListString("fav",favlist);
}

}
});

最佳答案

加载 Collection 夹后,您仅对 View 进行更改,但不将其放入favlist。下次当您将另一个项目添加到 Collection 夹时,您会将其放入 favlist 中,但它只包含这一项。之后,您将此单项列表保存到共享首选项,覆盖之前的内容。

当您从共享首选项加载数据时,将其放入 favlist 中。

顺便说一句,除非您的列表始终使用相同的数据以相同的顺序构建,否则在 Collection 夹中存储位置会导致问题。

关于java - 将列表中最喜欢的项目存储到 android 中的 SharedPreferences 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61373625/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com