共享首选项无法在片段

时间:2017-01-08 01:27:32

标签: android android-studio android-fragments sharedpreferences

我有这个应用程序,我试图在Fragment中存储按钮点击状态。但无论我尝试多少,似乎没有任何东西存储。我的代码肯定没问题。

public class ClubHome extends Fragment {

    ImageView bell,bellring;
    TextView beltext,belringtext;
    SharedPreferences saved_values;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.club_home, container, false);

        Bundle args = getArguments();
        final String index = args.getString("club", "Party");

        saved_values = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());

        bell= (ImageView) view.findViewById(R.id.bell);
        bellring= (ImageView) view.findViewById(R.id.bellring);
        beltext= (TextView) view.findViewById(R.id.bellmsg);
        belringtext= (TextView) view.findViewById(R.id.bellringmsg);

        bell.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                belringtext.setVisibility(View.VISIBLE);
                bellring.setVisibility(View.VISIBLE);
                bell.setVisibility(View.INVISIBLE);
                beltext.setVisibility(View.INVISIBLE);
                SharedPreferences.Editor editor=saved_values.edit();
                editor.putBoolean(index,true);
                editor.apply();
                editor.commit();
            }
        });

        bellring.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                belringtext.setVisibility(View.INVISIBLE);
                bellring.setVisibility(View.INVISIBLE);
                bell.setVisibility(View.VISIBLE);
                beltext.setVisibility(View.VISIBLE);
                SharedPreferences.Editor editor=saved_values.edit();
                editor.putBoolean(index,false);
                editor.apply();
                editor.commit();
            }
        });

        boolean stat = saved_values.getBoolean(index,false);

        if (stat){
            belringtext.setVisibility(View.INVISIBLE);
            bellring.setVisibility(View.INVISIBLE);
            bell.setVisibility(View.VISIBLE);
            beltext.setVisibility(View.VISIBLE);
        } else {
            belringtext.setVisibility(View.VISIBLE);
            bellring.setVisibility(View.VISIBLE);
            bell.setVisibility(View.INVISIBLE);
            beltext.setVisibility(View.INVISIBLE);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您在SharedPreference中保存数据的实施是错误的。

您需要先获取首选项属性。

SharedPreference pref = getActivity().getSharedPreferences("MY_PREFERENCES", Activity.MODE_PRIVATE);

现在在按钮的onClickListener中执行类似的操作以保存所需的值。

bell.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // .. Set visibility of items.
        pref.edit().putBoolean("INDEX", true).apply();
    }
});

要从INDEX获取SharedPreference的存储值,您需要执行以下操作

boolean indexStatus = pref.getBoolean("INDEX", false);  // false is the default value if nothing is returned.