Note that there are some explanatory texts on larger screens.

plurals
  1. POabout saving the status of the radiogroup in sharedpreferences
    primarykey
    data
    text
    <p>I have 2 activities here with the exact same xml layout, but with different id per item(every id should be unique right?) the problem is that I couldn't load the status of the radiogroup in the second activity, if I save it in the first one and vice versa (since I use the integer or the number from R.java)</p> <p>(btw, I didn't use radio buttons here, I just declare it, or should I use it?)</p> <p>Here's the code:</p> <pre><code>public class SharedPreferencesActivity extends Activity implements OnClickListener { /** Called when the activity is first created. */ String s; float number; EditText edittext; EditText editnum; RadioGroup rGroup; RadioButton radioa; RadioButton radiob; RadioButton radioc; SharedPreferences shared; Intent intent; double x = 0; boolean bool = true; int radiochecked; int radiocheckeddef; DecimalFormat df = new DecimalFormat("#.##"); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.page1); //final Context cont = SharedPreferencesActivity.this; edittext = (EditText) findViewById(R.id.text1); editnum = (EditText) findViewById(R.id.decimal1); Button save = (Button) findViewById(R.id.save1); Button load = (Button) findViewById(R.id.load1); Button page = (Button) findViewById(R.id.page1); save.setOnClickListener(this); load.setOnClickListener(this); page.setOnClickListener(this); rGroup = (RadioGroup)findViewById(R.id.radioGroup); radioa =(RadioButton) findViewById(R.id.radio1a); radiob =(RadioButton) findViewById(R.id.radio1b); radioc =(RadioButton) findViewById(R.id.radio1c); radiocheckeddef = rGroup.getCheckedRadioButtonId(); shared = getSharedPreferences("string", 0); } public void onClick(View v) { switch (v.getId()) { case R.id.save1: //radiochecked = rGroup.getCheckedRadioButtonId(); s = edittext.getText().toString(); number = Float.parseFloat(editnum.getText().toString()); radiochecked = rGroup.getCheckedRadioButtonId(); SharedPreferences.Editor editor = shared.edit(); editor.putString("sharedtext", s); editor.putFloat("sharednum", number); editor.putInt("sharedradio",radiochecked); editor.commit(); Toast.makeText(this, "save1", Toast.LENGTH_SHORT).show(); break; case R.id.load1: String returntext = shared.getString("sharedtext", "Returner fail"); float returnnum = shared.getFloat("sharednum", 0); int returnradio = shared.getInt("sharedradio", radiocheckeddef); edittext.setText(returntext); editnum.setText(String.valueOf(returnnum)); rGroup.check(returnradio); Toast.makeText(this, "load1", Toast.LENGTH_SHORT).show(); break; case R.id.page1: intent= new Intent(this,Shared2.class); startActivity(intent); break; } } </code></pre> <p>}</p> <p>and</p> <pre><code>public class Shared2 extends Activity implements OnClickListener { /** Called when the activity is first created. */ String string; float number; EditText edittext; EditText editnum; RadioGroup rGroup; RadioButton radioa; RadioButton radiob; RadioButton radioc; int radiochecked; int radiocheckeddef; SharedPreferences shared; Intent intent; double x = 0; DecimalFormat df = new DecimalFormat("#.##"); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.page2); edittext = (EditText)findViewById(R.id.text2); editnum = (EditText)findViewById(R.id.decimal2); Button save = (Button)findViewById(R.id.save2); Button load = (Button)findViewById(R.id.load2); Button page= (Button)findViewById(R.id.page2); save.setOnClickListener(this); load.setOnClickListener(this); page.setOnClickListener(this); shared = getSharedPreferences("string",0); rGroup = (RadioGroup)findViewById(R.id.radioGroup2); radioa =(RadioButton) findViewById(R.id.radio2a); radiob =(RadioButton) findViewById(R.id.radio2b); radioc =(RadioButton) findViewById(R.id.radio2c); radiocheckeddef = rGroup.getCheckedRadioButtonId(); } public void onClick(View v) { switch(v.getId()){ case R.id.save2: //get from edittext string = edittext.getText().toString(); number = Float.parseFloat(editnum.getText().toString()); radiochecked = rGroup.getCheckedRadioButtonId(); //put data into sharedpreferences SharedPreferences.Editor editor = shared.edit(); editor.putString("sharedtext",string); editor.putFloat("sharednum",number); editor.putInt("sharedradio",radiochecked); editor.commit(); Toast.makeText(this,"save2",Toast.LENGTH_SHORT).show(); break; case R.id.load2: //get data from sharedpreferences String returner = shared.getString("sharedtext","Returner fail"); float returnnum = shared.getFloat("sharednum",0); int returnradio = shared.getInt("sharedradio", radiocheckeddef); //put data into edittext fields edittext.setText(returner); editnum.setText(String.valueOf(returnnum)); rGroup.check(returnradio); Toast.makeText(this,"load2",Toast.LENGTH_SHORT).show(); break; case R.id.page2: intent= new Intent(this,SharedPreferencesActivity.class); startActivity(intent); break; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload