I use 6 checkboxes in my app. They all use the same methods because they all work similarly. However, I'm trying to save the state of the checkboxes in the users shared preferences so they don't have to check them next time they use the app. But this one checkbox, "inputcheerControl," just won't stick in the shared preferences like the others. I can't seem to figure out the issue.
Here is a quick test practice to maybe help show what is going on. I maniuplate the checkbox and use a button to output the commands above. ^
System.out.println(Prefs.getBoolean("Cheer_Control", false));
System.out.println(inputcheerControl.isChecked());
*snip touch output
//start of app. checkbox not enabled
06-07 17:24:39.801 30014-30014/com.ritchie.irldevapp I/System.out: false
06-07 17:24:39.801 30014-30014/com.ritchie.irldevapp I/System.out: false
*snip touch output
//ticked checkbox manually and test. not in shared preferences yet.
06-07 17:24:51.513 30014-30014/com.ritchie.irldevapp I/System.out: false
06-07 17:24:51.523 30014-30014/com.ritchie.irldevapp I/System.out: true
*snip touch output
//saved settings and tested
06-07 17:25:01.774 30014-30014/com.ritchie.irldevapp I/System.out: false
06-07 17:25:01.774 30014-30014/com.ritchie.irldevapp I/System.out: true
Here is the code:
*snip
@SuppressLint("Registered")
class PrefsApplication extends MainActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initialize the Prefs class
new Prefs.Builder()
.setContext(this)
.setMode(ContextWrapper.MODE_PRIVATE)
.setPrefsName(getPackageName())
.setUseDefaultSharedPreference(true)
.build();
}
}
public class MainActivity extends AppCompatActivity {
AsyncTask ircThread;
*snip
// checkboxes
CheckBox inputMods;
CheckBox inputSubs;
CheckBox inputVIP;
CheckBox inputBits;
CheckBox inputViewers;
CheckBox inputcheerControl;
//return mainactivity instance
private static MainActivity instance;
public static MainActivity getInstance() {
return instance;
}
public void setSettings() {
System.out.println("Saving user settings");
Prefs.putString("Username", String.valueOf(inputBotUser.getText()));
Prefs.putString("Oauth", String.valueOf(inputBotOauth.getText()));
Prefs.putString("Channel", String.valueOf(inputBotChan.getText()));
Prefs.putInt("Bits_Amount", Integer.parseInt(String.valueOf(inputBitsAmnt.getText())));
Prefs.putBoolean("Access_Mods", inputMods.isChecked());
Prefs.putBoolean("Access_Subs", inputSubs.isChecked());
Prefs.putBoolean("Access_VIP", inputVIP.isChecked());
Prefs.putBoolean("Access_Bits", inputBits.isChecked());
Prefs.putBoolean("Access_Viewers", inputViewers.isChecked());
Prefs.putBoolean("Cheer_Control", inputcheerControl.isChecked());
}
public void displaySettings() {
System.out.println("Displaying user settings");
//textviews
if (Prefs.contains("Username")) { inputBotUser.setText(Prefs.getString("Username", "")); }
if (Prefs.contains("Oauth")) { inputBotOauth.setText(Prefs.getString("Oauth", "")); }
if (Prefs.contains("Channel")) {inputBotChan.setText(Prefs.getString("Channel", "")); }
if (Prefs.contains("Bits_Amount")) {inputBitsAmnt.setText(String.valueOf(Prefs.getInt("Bits_Amount", 0))); }
//checkboxes
if (Prefs.contains("Access_Mods") && Prefs.getBoolean("Access_Mods", false)) { inputMods.setChecked(true); } else { inputMods.setChecked(false); }
if (Prefs.contains("Access_Subs") && Prefs.getBoolean("Access_Subs", false)) { inputSubs.setChecked(true); } else { inputSubs.setChecked(false); }
if (Prefs.contains("Access_VIP") && Prefs.getBoolean("Access_VIP", false)) { inputVIP.setChecked(true); } else { inputVIP.setChecked(false); }
if (Prefs.contains("Access_Bits") && Prefs.getBoolean("Access_Bits", false)) { inputBits.setChecked(true); } else { inputBits.setChecked(false); }
if (Prefs.contains("Access_Viewers") && Prefs.getBoolean("Access_Viewers", false)) { inputViewers.setChecked(true); } else { inputViewers.setChecked(false); }
if (Prefs.contains("Cheer_Control") && Prefs.getBoolean("Cheer_Control", false)) { inputcheerControl.setChecked(true); System.out.println("<<<< true"); } else { inputcheerControl.setChecked(false); System.out.println("<<<< false"); }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
instance = this;
//vars
new Prefs.Builder()
.setContext(this)
.setMode(ContextWrapper.MODE_PRIVATE)
.setPrefsName(getPackageName())
.setUseDefaultSharedPreference(true)
.build();
//inputs
// text inputs
*snip
// buttons
inputbtnIRC = (Button) findViewById(R.id.button_irc);
inputbtnBT = (Button) findViewById(R.id.button_bt);
inputbtnPause = (Button) findViewById(R.id.button_pause);
inputbtnSave = (Button) findViewById(R.id.button_resetsys);
// checkboxes
inputMods = (CheckBox) findViewById(R.id.checkBox_mods);
inputSubs = (CheckBox) findViewById(R.id.checkBox_subs);
inputVIP = (CheckBox) findViewById(R.id.checkBox_vip);
inputBits = (CheckBox) findViewById(R.id.checkBox_bits);
inputViewers = (CheckBox) findViewById(R.id.checkBox_viewers);
inputcheerControl = (CheckBox) findViewById(R.id.checkBox_cheerControl);
//Display saved prefs
displaySettings();
*snip (your post has too much code)
// BT
inputbtnBT.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View v) {
//BT button pressed
System.out.println(Prefs.getBoolean("Cheer_Control", false));
System.out.println(inputcheerControl.isChecked());
//displaySettings();
}
});
// save
inputbtnPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//save button pressed
setSettings();
}
});
// Reset
inputbtnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Reset button pressed
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
System.out.println("Back button pressed. Saving settings.");
setSettings();
}
*snip
Aucun commentaire:
Enregistrer un commentaire