Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android在SharedReferences中保留StringSet不起作用_Java_Android_Set_Sharedpreferences - Fatal编程技术网

Java Android在SharedReferences中保留StringSet不起作用

Java Android在SharedReferences中保留StringSet不起作用,java,android,set,sharedpreferences,Java,Android,Set,Sharedpreferences,我想在SharedReferences中保留“收藏夹”记录,但出现了一些问题。在独立于此的SharedReferences中,我正在做的事情总是有一个记录。我决定添加的第一个。我正在检查,但删除它效果很好,但在完全重新启动或重新插入应用程序后,问题又出现了。我添加3条记录,重新启动应用程序,有我添加的第一条。我删除它,添加另一个,重新启动应用程序,还有第一个来自午餐前。我删除它,重新启动应用程序,这一个仍然存在。我删除它,添加另一个,重新启动应用程序,上一个在列表中。我试过,例如: 我的适配器,

我想在SharedReferences中保留“收藏夹”记录,但出现了一些问题。在独立于此的SharedReferences中,我正在做的事情总是有一个记录。我决定添加的第一个。我正在检查,但删除它效果很好,但在完全重新启动或重新插入应用程序后,问题又出现了。我添加3条记录,重新启动应用程序,有我添加的第一条。我删除它,添加另一个,重新启动应用程序,还有第一个来自午餐前。我删除它,重新启动应用程序,这一个仍然存在。我删除它,添加另一个,重新启动应用程序,上一个在列表中。我试过,例如:

我的适配器,它显示一条我要保留的记录:

public class DayFavouriteAdapter extends ArrayAdapter<String> {
    private Context context;
    private String[] values;

    public DayFavouriteAdapter(Context context, String[] values) {
        super(context, R.layout.day_favourite, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View day = inflater.inflate(R.layout.day_favourite, parent, false);

        final SharedPreferences prefs = Data.getPreferences(context, null);
        final Set<String> favs = prefs.getStringSet(Data.favourites, new HashSet<String>());

        day.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                final Activity activity = FavouriteActivity.getInstance();
                AlertDialog.Builder deleting = new AlertDialog.Builder(activity);
                deleting.setTitle("Deleting");
                deleting.setMessage("Are you sure?");
                deleting.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        boolean a = favs.remove(favs.toArray(new String[]{})[position]); //a is always true, misstake is impossible
                        Log.d("AC", favs.toString() + ""); //returns correct result
                        SharedPreferences.Editor editor = prefs.edit();
                        editor.putStringSet(Data.favourites, favs);
                        editor.commit();
                        editor.apply();
                        Log.d("AC", prefs.getStringSet(Data.favourites, new HashSet<String>()) + ""); //returns correct result
                        activity.recreate(); //after this, everything is cool.
                    }
                });
                deleting.setNegativeButton("No", null);
                deleting.show();
                return false;
            }
        });
        return day;
    }
}

我正在使用一些方法来做这件事,但任何方法都很有效

你说“问题回来了”,哪个问题。请添加更多explanation@AndroidEnthusiast我补充了更多你喜欢的解释。
public abstract class Data {
    public static final String favourites = "favourites";
    public static final SharedPreferences getPreferences(Context context, String s) {
        if (s==null) {
            return PreferenceManager.getDefaultSharedPreferences(context);
        }
        return context.getSharedPreferences(s, Activity.MODE_PRIVATE);
    }
}