I am having issue in accessing String values from two activities through shared preferences. I think i am making some blunders can any one help me out with it? Basically what i want from activity 1 and activity 2 is to get integer values from user using edit text, store them in shared preferences(restored on application relaunch). now using values of activity 1 and 2 i want to perform calculation in result activity.
my problems:-
how to access shared preferences in result activity?
values are stored in string can i perform calculation on that or do i have to convert them to int in results activity? if yes how to convert them to int (is Integer.valueOff will work?)
Activity 1
public class Abc extends Activity {
Button one2five, save1;
EditText edtA, edtB, edtC, edtD, edtE, edtF;
String tA, tsB, tsC, tsD, tsE, tsF;
int tB, tC, tD, tE, tF;
public static String FILE1 = "MyPrefsFile";
SharedPreferences abcPref;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.abc);
one2five = (Button) findViewById(R.id.btp1);
save1 = (Button) findViewById(R.id.btps1);
edtA = (EditText) findViewById(R.id.etA);
edtB = (EditText) findViewById(R.id.etB);
edtC = (EditText) findViewById(R.id.etC);
edtD = (EditText) findViewById(R.id.etD);
edtE = (EditText) findViewById(R.id.etE);
edtF = (EditText) findViewById(R.id.etF);
abcPref = getSharedPreferences("FILE1", 0);
edtA.setText(abcPref.getString("tA", ""));
edtB.setText(abcPref.getString("tsB", ""));
edtC.setText(abcPref.getString("tsC", ""));
edtD.setText(abcPref.getString("tsD", ""));
edtE.setText(abcPref.getString("tsE", ""));
edtF.setText(abcPref.getString("tsF", ""));
one2five.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ((!edtA.getText().toString().equals(""))
&& (!edtB.getText().toString().equals(""))
&& (!edtC.getText().toString().equals(""))
&& (!edtD.getText().toString().equals(""))
&& (!edtE.getText().toString().equals(""))
&& (!edtF.getText().toString().equals(""))) {
Intent openg2j = new Intent("com.sports.sport.G2J");
startActivity(openg2j);
}
}
});
save1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
abcPref = getSharedPreferences("FILE1", 0);
SharedPreferences.Editor editor = abcPref.edit();
editor.putString("tA", edtA.getText().toString());
editor.putString("tsB", edtB.getText().toString());
editor.putString("tsC", edtC.getText().toString());
editor.putString("tsD", edtD.getText().toString());
editor.putString("tsE", edtE.getText().toString());
editor.putString("tsF", edtF.getText().toString());
editor.commit();
}
});
}
}
Activity 2
public class G2J extends Activity {
Button two2five, save2;
EditText edtG, edtH, edtI, edtJ, edtK;
int tG, tH, tI, tJ, tK;
String tsG, tsH, tsI, tsJ, tsK;
public static String FileP2 = "MyPrefsFile";
SharedPreferences abcPref;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.g2j);
two2five = (Button) findViewById(R.id.btp2);
save2 = (Button) findViewById(R.id.btps2);
edtG = (EditText) findViewById(R.id.etG);
edtH = (EditText) findViewById(R.id.etH);
edtI = (EditText) findViewById(R.id.etI);
edtJ = (EditText) findViewById(R.id.etJ);
edtK = (EditText) findViewById(R.id.etK);
abcPref = getSharedPreferences("FileP2", 0);
edtG.setText(abcPref.getString("tsG", ""));
edtH.setText(abcPref.getString("tsH", ""));
edtI.setText(abcPref.getString("tsI", ""));
edtJ.setText(abcPref.getString("tsJ", ""));
edtK.setText(abcPref.getString("tsK", ""));
two2five.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ((!edtG.getText().toString().equals(""))
&& (!edtH.getText().toString().equals(""))
&& (!edtI.getText().toString().equals(""))
&& (!edtJ.getText().toString().equals(""))
&& (!edtK.getText().toString().equals(""))) {
// TODO Auto-generated method stub
Intent openl2p = new Intent("com.sports.sport.Results");
startActivity(openl2p);
}
}
});
save2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
abcPref = G2J.this.getSharedPreferences(FileP2, 0);
SharedPreferences.Editor editor = abcPref.edit();
editor.putString("tsG", edtG.getText().toString());
editor.putString("tsH", edtH.getText().toString());
editor.putString("tsI", edtI.getText().toString());
editor.putString("tsJ", edtJ.getText().toString());
editor.putString("tsK", edtK.getText().toString());
editor.commit();
}
});
}
}
Finally Results Activity
public class Results extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.resultmain);
SharedPreferences abcPref= this.getSharedPreferences(FILE1,0);
}
}