0

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:-

  1. how to access shared preferences in result activity?

  2. 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);

    }
}
Monika
  • 74
  • 7
  • see this might help u http://stackoverflow.com/questions/3783848/android-possible-to-have-multiple-distinct-shared-preferences-per-app – Developer Sep 13 '13 at 09:55
  • import android.preference.PreferenceManager; SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); // then you use prefs.getBoolean("keystring", true); – Developer Sep 13 '13 at 09:58
  • thanks gaurav, could you rewrite result activity for an idea. – Monika Sep 13 '13 at 09:59
  • have checked this code this is working or not .this will be the class in which u have created the shared pref – Developer Sep 13 '13 at 10:03
  • tarun example is best you can go with that – Developer Sep 13 '13 at 10:19

4 Answers4

1

In your Abc activiy repalce

abcPref = getSharedPreferences("FILE1", 0);

with

 abcPref = getSharedPreferences(FILE1, 0);

In your G2J activiy repalce

abcPref = getSharedPreferences("FileP2", 0);

with

 abcPref = getSharedPreferences(FileP2, 0);

In Results Activity define

public static String FILE1= "MyPrefsFile";

To perform caluclation on String values you can parse the string like int value = Integer.parseInt(abcPref.getString("tsG")); and so on

If you just want to accept integer values from editText then in your xcm add:

android:inputType="number"

<EditText
android:id="@+id/edtA"
android:inputType="number"
/>
Tarun
  • 13,727
  • 8
  • 42
  • 57
  • Amazing thanks a ton Tarun, and to access FileP2 in Results activity?? – Monika Sep 13 '13 at 10:11
  • Your value of FileP2 is same which is "MyPrefsFile". Since you are using getSharedPreferences you can share this preference file with any context. – Tarun Sep 13 '13 at 10:13
  • correct me if i am wrong:- in my case "MyPrefsFile" is folder wherein FILE1 and FileP2 are files which can be access in any activity.. or having same file name will be easier and not erase any data?? – Monika Sep 13 '13 at 10:16
  • oh it means main file is "MyPrefsFile" and i call it whatever in an activity, to access data of "MyPrefsFile" i need to give key (tsA, tsG etc...) am i correct?? – Monika Sep 13 '13 at 10:18
  • `SharedPreferences getSharedPreferences (String name, int mode)` Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values. – Tarun Sep 13 '13 at 10:18
  • If you dont want to maintain pref file on your own then you can instead use PreferenceManager.getDefaultSharedPreferences(conttext); – Tarun Sep 13 '13 at 10:19
  • @Monika have you got the solution of your problem or not – Developer Sep 13 '13 at 11:24
  • i m compiling complete app looks now its working if any problem occurs i will contact you here – Monika Sep 13 '13 at 12:15
0
try this way
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);



             int a = Integer.parseInt(abcPref.getString("tA", ""));
             int b = Integer.parseInt(abcPref.getString("tsB", ""));
             int c = Integer.parseInt(abcPref.getString("tsC", ""));
             int d = Integer.parseInt(abcPref.getString("tsD", ""));
             int e = Integer.parseInt(abcPref.getString("tsE", ""));
             int f = Integer.parseInt(abcPref.getString("tsF", ""));

          // Your operations



        }
    }
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
0

I suggest you use getDefaultSharedPreferences() in both activities instead of getSharedPreferences(). That way there's no risk that you make a typo in the name of the preferences file to be used.

Litrik De Roy
  • 823
  • 5
  • 9
0

1st you should check for null pointer exception as, if the user saves it then you're committing editor else you're not saving any string values. So if user doesn't save it and still in result activity you're getting string from your preference like abcPref.getString then it will throw a NullPointerException. Also you can perform parsing of String to Integer using Interger.parseInt(abcPref.getString("tA","" + 0)); and so on...

imthegiga
  • 1,126
  • 8
  • 13
  • You can handle Null check as per your requirement. You can parse string to integer using last line. That line just returns an integer so just assign it to any integer variable that's it. Same for your second preference. – imthegiga Sep 13 '13 at 10:06