check radio button missed value - android

i have an issue, to memorize values of EditText and RadioButtons i add #onResume and #onPause methods, it works fine for Edit Text but the problem is that the radio buttons appear checked but when i click to register the value into database, i have message (No value detected), what should i do to resolve the issue ? is there a solution !!!
#Override
protected void onPause()
{
super.onPause();
SharedPreferences prefs3 = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs3.edit();
editor.putBoolean("questionA", rm_13_1.isChecked());
editor.putBoolean("questionB", rm_13_2.isChecked());
editor.putBoolean("questionC", rm_14_1.isChecked());
editor.putBoolean("questionD", rm_14_2.isChecked());
editor.putBoolean("questionE", rm_14_3.isChecked());
// Commit the edits!
editor.commit();
}
#Override
protected void onResume(){
super.onResume();
SharedPreferences prefs3 = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
rm_13_1 = (RadioButton)findViewById(R.id.rm_13_1) ;
rm_13_2 = (RadioButton)findViewById(R.id.rm_13_2);
rm_14_1 = (RadioButton)findViewById(R.id.rm_14_1) ;
rm_14_2 = (RadioButton)findViewById(R.id.rm_14_2);
rm_14_3 = (RadioButton)findViewById(R.id.rm_14_2);
Boolean rm_13_1A = false;
Boolean rm_13_2A = false;
Boolean rm_14_1A = false;
Boolean rm_14_2A = false;
Boolean rm_14_3A = false;
rm_13_1A = prefs3.getBoolean("questionA",false);
rm_13_2A = prefs3.getBoolean("questionB",false);
rm_14_1A = prefs3.getBoolean("questionC",false);
rm_14_2A = prefs3.getBoolean("questionD",false);
rm_14_3A = prefs3.getBoolean("questionE",false);
rm_13_1.setChecked(rm_13_1A );
rm_13_2.setChecked(rm_13_2A );
rm_14_1.setChecked(rm_14_1A );
rm_14_2.setChecked(rm_14_2A );
rm_14_3.setChecked(rm_14_3A );
}

pref=PreferenceManager.getDefaultSharedPreference(this)
try

Related

Cant save string with shared preferences

I have two strings that I want to save them I wrote the code as shown below
public class MainActivity extends Activity {
Button result;
EditText b951, b9511, sum95, t95, p95
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LoadPreferences();
result = (Button)findViewById(R.id.btn1);
b951 = (EditText)findViewById(R.id.b951);
b9511 = (EditText)findViewById(R.id.b9511);
sum95 = (EditText)findViewById(R.id.sum95);
p95 = (EditText)findViewById(R.id.p95);
t95 = (EditText)findViewById(R.id.t95);
}
public void close (View v){
SavePreferences("p2b951", b951.getText().toString());
SavePreferences("p2b9511", b9511.getText().toString());
finish();
}
private void SavePreferences(String key, String value){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
private void LoadPreferences(){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String p1b951 = sharedPreferences.getString("p2b951", "1");
String p1b9511 = sharedPreferences.getString("p2b9511", "1");
b951.setText(p1b951);
b9511.setText(p1b9511);
}
public void result (View v) {
try {
int ib951 = Integer.parseInt(b951.getText().toString());
int ib9511 = Integer.parseInt(b9511.getText().toString());
int iisum95 = (ib9511-ib951);
sum95.setText(String.valueOf(iisum95));
int isum95 = Integer.parseInt(sum95.getText().toString());
int ip95 = Integer.parseInt(p95.getText().toString());
int pp95 = (isum95*ip95);
t95.setText(String.valueOf(pp95));
}
catch (Exception e) {
e.printStackTrace();
}
}
}
But it seems there is a problem with this two lines:
b951.setText(p1b951);
b9511.setText(p1b9511);
I tried this
b951.setText(String.valueOf.p1b951);
b9511.setText(String.valueOf.p1b9511);
Also the problems still exist
When I open the application and when the loadpreferences is called the app will force close
The logcat show me that the error is with this two lines for sure it's just with the first line coz he didn't get the second but they are the same so they are wrong wroted
Any help??
Change onCreate() as follows:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
result = (Button)findViewById(R.id.btn1);
b951 = (EditText)findViewById(R.id.b951);
b9511 = (EditText)findViewById(R.id.b9511);
sum95 = (EditText)findViewById(R.id.sum95);
p95 = (EditText)findViewById(R.id.p95);
t95 = (EditText)findViewById(R.id.t95);
LoadPreferences();
}
This is because you are using the EditTexts in the LoadPreferences() without calling findViewById() on the EditTexts.
You can follow and help from like codes. such as
Setting values in Preference:
SharedPreferences.Editor editor = getSharedPreferences(MODE_PRIVATE).edit();
editor.putString("name", "Elena");
editor.putInt("idName", 12);
editor.commit();
Retrieve data from preference:
SharedPreferences prefs = getSharedPreferences(MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
int idName = prefs.getInt("idName", 0); //0 is the default value.
}
it any confused about this then follow this link

Android SharedPreferences not loading and saving properly

I've been getting null returns from getting strings from my saved preferences. I'm not sure how savedpreferences worked but my understanding was that when call a sharedpreferences, it creates the keypair file on the phone so you can come back to it later.
My program is essentially a string creation application. When you press a button, it creates a string to send as an sms. My settings activity page has four edittexts that save whatever is inside them with a buttonclick and returns to the main activity. The final button creates a String by getting the value from the keyvalue pair and constructs the message. However, I've always gotten null for each of the values.
Heres the code for the settings page and then the main page. Please ask if I could add more, I didn't add ALL of the code, just the sharedpreferences portions.
public SharedPreferences sp;
public Editor e;
public void savethethings(){ //run this when enter is pressed/savew
EditText smsintro_hint = (EditText) findViewById(R.id.settings_smsintro_hint);
EditText smsbody_hint = (EditText) findViewById(R.id.settings_smsbody_hint);
EditText checkboxbody_hint = (EditText) findViewById(R.id.settings_checkboxbody_hint);
EditText checkboxbody_description_hint = (EditText) findViewById(R.id.settings_checkboxbody_description_hint);
String introstring = smsintro_hint.getText().toString();
String bodystring = smsbody_hint.getText().toString();
String checkboxbodystring = checkboxbody_hint.getText().toString();
String checkboxdescriptionstring = checkboxbody_description_hint.getText().toString();
e.putString("intro", introstring);
e.commit(); // you forgot to commit
if(!bodystring.isEmpty())
{
e.putString("body", bodystring);
e.commit();
}
if(!checkboxbodystring.isEmpty())
{
e.putString("checkbody", checkboxbodystring);
e.commit();
}
if(!checkboxdescriptionstring.isEmpty())
{
e.putString("checkboxdescr", checkboxdescriptionstring);
e.commit();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.settingmenu);
//SP
sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); // forget about
// named preferences - get the default ones and finish with it
e = sp.edit();
Button tt = (Button)findViewById(R.id.savebutton);
tt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
public void save(View view)
{
//THINGS HAPPEN HERE WITH SHARED PREFERENCES :(
savethethings();
this.finish();
return;
}
public String finishedtext(String userstring)
{
smsintroduction = (sp.getString("intro", ""));
smsbody = (sp.getString("body", ""));
checkboxtext = (sp.getString("checkbody", ""));
checkboxmessage = (sp.getString("checkboxdescr", ""));
if(smsintroduction.isEmpty())
{
if(smsbody.isEmpty())
{
if(checkboxtext.isEmpty())
{
if(checkboxmessage.isEmpty()) //topkek for most AND statements Ive ever put in in if/then form
{
//Essentially the DEFAULT if they're ALL null
smsbody = "Hi "+ userstring +"! This is coming from jake's phone and it wants to send a text so we can talk or whatever. ";
}
}
}
}
Toast.makeText( this, "Creating text, then press send!", Toast.LENGTH_LONG).show();
String thetext = "";
thetext = smsintroduction + " " + smsbody + " " + checkboxtext;
return thetext;
}
public void savethethings(){ //run this when enter is pressed/savew
EditText smsintro_hint = (EditText) findViewById(R.id.settings_smsintro_hint);
EditText smsbody_hint = (EditText) findViewById(R.id.settings_smsbody_hint);
EditText checkboxbody_hint = (EditText) findViewById(R.id.settings_checkboxbody_hint);
EditText checkboxbody_description_hint = (EditText) findViewById(R.id.settings_checkboxbody_description_hint);
String introstring = smsintro_hint.getText().toString();
String bodystring = smsbody_hint.getText().toString();
String checkboxbodystring = checkboxbody_hint.getText().toString();
String checkboxdescriptionstring = checkboxbody_description_hint.getText().toString();
// if(!introstring.isEmpty()) //if the fields are NOT empty, they should get saved.
// {
e.putString("intro", introstring);
e.commit(); // you forgot to commit
if(!bodystring.isEmpty())
{
e.putString("body", bodystring);
e.commit();
}
if(!checkboxbodystring.isEmpty())
{
e.putString("checkbody", checkboxbodystring);
e.commit();
}
if(!checkboxdescriptionstring.isEmpty())
{
e.putString("checkboxdescr", checkboxdescriptionstring);
e.commit();
}
}
Create java class named SessionManger and put all your methods for setting and getting SharedPreferences values. When you want to save value use the object of this class and set and get the values.
Sample code given below.
public class SessionManager {
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;
int PRIVATE_MODE = 0;
public SessionManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences("name_that_you_use", PRIVATE_MODE);
editor = pref.edit();
editor.apply();
}
public void setIntroMessage(String data) {
editor.putString("intro", data);
editor.commit();
}
public String getIntroMessage() {
return pref.getString("intro", null);
}
}

Does this snippet cause an infinite loop?

I'm not sure. I created this code that crashes only on device < 2.3.x. Is this code causes a loop?
SharedPreferences prefs3 = PreferenceManager.getDefaultSharedPreferences(this);
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String listpref) {
preferenze();
} };
private void preferenze() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
CheckboxPreference = prefs.getBoolean("checkboxPref", true);
ListPreference = prefs.getString("listpref", "");
numeronotifiche = prefs.getString("notify", "");
Sound = prefs.getString("sound", "");
barranotifiche = prefs.getBoolean("keep", false);

does change in preferences commit then and there or need to call activity again?

I have this code:
public static final String PREFS_NAME = "MyPrefsFile";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
settings = getSharedPreferences(PREFS_NAME, 0);
boolean hasLoggedIn = settings.getBoolean("hasLoggedIn", false);
if (!((LoginButton.email).equals(""))) {
//settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
// Set "hasLoggedIn" to true
editor.putBoolean("hasLoggedIn", true);
// Commit the edits!
editor.commit();
Log.d("hasLoggedIn in email check = ", hasLoggedIn + "");
}
}
even after entering the if, the last Log gives me hadLoggedIn as false.
Somewhere in the same activity i got the same code of editing which works fine, but the only difference is i never used it as soon as it got edited, i used it when the activity is called again.
You will again have to open sharedpreference for reading.
your hasLoggedIn still contains old value.
I have updated your code as below.
public static final String PREFS_NAME = "MyPrefsFile";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
settings = getSharedPreferences(PREFS_NAME, 0);
boolean hasLoggedIn = settings.getBoolean("hasLoggedIn", false);
if (!((LoginButton.email).equals(""))) {
//settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
// Set "hasLoggedIn" to true
editor.putBoolean("hasLoggedIn", true);
// Commit the edits!
editor.commit();
//You forgot write following line.
hasLoggedIn = getSharedPreferences(PREFS_NAME, 0).getBoolean("hasLoggedIn", false);
Log.d("hasLoggedIn in email check = ", hasLoggedIn + "");
}
}

Android - SharedPreference.getBoolean retrieving false even if i am storing true?

I am using SharedPreference to store the state of checkboxes but even i am storing true in it , its still retrieving false.
Here is my code -
#Override
public void onPause()
{
super.onPause();
saveState();
}
#Override
public void onResume()
{
super.onResume();
loadState();
}
#Override
public void onRestart()
{
super.onRestart();
loadState();
}
public void saveState()
{
SharedPreferences sp = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
for(int i = 0; i < itemCheck.length; i++)
{
Boolean b = itemCheck[i];
Log.e(TAG, b.toString());
editor.putBoolean(i+"", itemCheck[i]);
}
}
public void loadState()
{
SharedPreferences sp = getPreferences(Context.MODE_PRIVATE);
for(int i = 0; i < itemCheck.length; i++)
{
itemCheck[i] = sp.getBoolean(i+"", false);
Boolean b = itemCheck[i];
Log.e(TAG, b.toString());
}
for(int i = 0; i < itemCheck.length; i++)
{
lv.setItemChecked(i, itemCheck[i]);
}
}
Its giving me false because i set false as a default value in getBoolean which should be returned in the absence of predefined key. Please take a look and tell me what i did wrong. Thanks
You never call commit() on your editor I think :)
Try this:
public void saveState()
{
SharedPreferences sp = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
for(int i = 0; i < itemCheck.length; i++)
{
Boolean b = itemCheck[i];
Log.e(TAG, b.toString());
editor.putBoolean(i+"", itemCheck[i]);
}
editor.commit();
}
use editor.commit() after editor.putBoolean(i+"", itemCheck[i]);

Categories

Resources