我的应用程序中的SharedPreferences中的checkBoxed有问题。我使用完全相同的代码五个复选框。一个复选框工作正常。现在,其他四个复选框中的三个将像第四个复选框一样。我的意思是当我检查第四个复选框并保存首选项时,其他3个复选框将与第四个相同。这是代码,其中“第四个”复选框是声音,而正常工作的复选框是建议的帖子。
SaveProfileState类
public class SaveProfileState {
private final static String THE_RATIO="10";
private final static String TEXT_POSTS="true";
private final static String EVENT_POSTS="true";
private final static String PHOTO_POSTS="true";
private final static String SOUND="true";
private final static String RECOMMENDED_POSTS="false";
private SharedPreferences userPrefs;// save data on the device
private static final String PREFS_NAME="userProfileState2";//definition for shared
public SaveProfileState(Context context){
userPrefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
}
public SaveProfileState(){
}
public void saveChangesState(int ratio, boolean textPosts, boolean eventPosts, boolean photoPosts, boolean sound, boolean recommendedPosts){
SharedPreferences.Editor editor = userPrefs.edit();
editor.putInt(THE_RATIO, ratio);
editor.putBoolean(TEXT_POSTS, textPosts);
editor.putBoolean(EVENT_POSTS, eventPosts);
editor.putBoolean(PHOTO_POSTS, photoPosts);
editor.putBoolean(SOUND, sound);
editor.putBoolean(RECOMMENDED_POSTS, recommendedPosts);
editor.commit();
}
public int getRatio(){
return userPrefs.getInt(THE_RATIO, 0);
}
public boolean getTextPostsCheck(){
return userPrefs.getBoolean(TEXT_POSTS, true);
}
public boolean getEventPostsCheck(){
return userPrefs.getBoolean(EVENT_POSTS, true);
}
public boolean getPhotoPostsCheck(){
return userPrefs.getBoolean(PHOTO_POSTS, true);
}
public boolean getSoundCheck(){
return userPrefs.getBoolean(SOUND, true);
}
public boolean getRecommendedPostsCheck(){
return userPrefs.getBoolean(RECOMMENDED_POSTS, false);
}
}
个人资料类
public class Profile extends AppCompatActivity {
private SeekBar seekBar;
private TextView textView;
private CheckBox textPosts;
private CheckBox eventPosts;
private CheckBox photoPosts;
private CheckBox sounds;
private CheckBox recommendedPosts;
private boolean textPost;
private boolean eventPost;
private boolean photoPost;
private boolean sound;
private boolean recommendedPost;
private void initializeVariables() {
seekBar = (SeekBar) findViewById(R.id.seekBar1);
textView = (TextView) findViewById(R.id.textView1);
textPosts = (CheckBox) findViewById(R.id.textPostsOption);
eventPosts = (CheckBox) findViewById(R.id.eventPostsOption);
photoPosts = (CheckBox) findViewById(R.id.photoPostsOption);
sounds = (CheckBox) findViewById(R.id.soundOption);
recommendedPosts = (CheckBox) findViewById(R.id.recommendedBox);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
initializeVariables();
SaveProfileState sps = new SaveProfileState(this);
textPost = sps.getTextPostsCheck();
textPosts.setChecked(sps.getTextPostsCheck());
eventPosts.setChecked(sps.getEventPostsCheck());
sounds.setChecked(sps.getSoundCheck());
photoPosts.setChecked(sps.getPhotoPostsCheck());
recommendedPosts.setChecked(sps.getRecommendedPostsCheck());
final Button button = (Button) findViewById(R.id.saveChanges);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
saveChanges();
}
});
}
public void saveChanges(){
SaveProfileState sps = new SaveProfileState(this);
sps.saveChangesState(ratio, textPosts.isChecked(), eventPosts.isChecked(), photoPosts.isChecked(), sounds.isChecked(), recommendedPosts.isChecked());
}
}
类Profile的XML
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:facebook="http://schemas.android.com/tools"
android:id="@+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none"
android:background="@color/blue">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View only recommended Posts"
android:id="@+id/recommendedBox"
android:layout_below="@+id/textView1"
android:layout_alignParentStart="true"
android:checked="false" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text Posts"
android:id="@+id/textPostsOption"
android:layout_below="@+id/postKinds"
android:layout_alignStart="@+id/textView1"
android:checked="true" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Event Posts"
android:id="@+id/eventPostsOption"
android:layout_below="@+id/textPostsOption"
android:layout_alignStart="@+id/textPostsOption"
android:checked="true" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Photo Posts"
android:id="@+id/photoPostsOption"
android:layout_below="@+id/eventPostsOption"
android:layout_alignStart="@+id/eventPostsOption"
android:checked="true" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sound"
android:id="@+id/soundOption"
android:layout_below="@+id/photoPostsOption"
android:layout_alignParentStart="true"
android:checked="true" />
<Button
android:text="Save Changes"
android:id="@+id/saveChanges"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:background="@drawable/go_btn"
android:padding="6dp"
android:textSize="13dp"
android:layout_marginTop="5dp"
android:layout_below="@+id/login_button"
/>
这是一张它的样子照片:
我找不到问题,我发现它很奇怪,因为相同的代码适用于一个复选框而不适用于其他复选框。
答案 0 :(得分:1)
在SaveProfileState
中,在SharedPreference中保存的密钥必须是唯一的。
private final static String TEXT_POSTS="textPost"; // key 1
private final static String EVENT_POSTS="eventPost"; // key 2
private final static String PHOTO_POSTS="photoPost"; // key 3
private final static String SOUND="sound"; // key 4
基本上您将所有四种状态保存在单键中。
private final static String TEXT_POSTS="true"; // key 1
private final static String EVENT_POSTS="true"; // key 2
private final static String PHOTO_POSTS="true"; // key 3
private final static String SOUND="true"; // key 4