开发者

save the data using shared preference in android

开发者 https://www.devze.com 2023-03-05 12:36 出处:网络
i am new developer in android applications.i would like to save the data using shared preference concept.i am saving the data in one activity and get the same data in another activity.here i would lik

i am new developer in android applications.i would like to save the data using shared preference concept.i am saving the data in one activity and get the same data in another activity.here i would like to send the String a[]={"one","two","three"} one activity to another activity.i have written code as follows

Main1.java

public class Main1 extends Activity
 {

  @Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.l开发者_Python百科ayout.main);

    SharedPreferences shp=getSharedPreferences("TEXT", 0);
    final Editor et=shp.edit();

    ((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            String s1=((EditText)findViewById(R.id.editText1)).getText().toString();
            et.putString("DATA", s1);

            String s2[]={"one","two","three"};

            //here i would like to save the string array

            et.commit();
            Intent it=new Intent(Main1.this,Main2.class);
            startActivity(it);

        }
    });


}

Main2.java

@Override

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);
    String kk=getSharedPreferences("TEXT", 0).getString("DATA", null);

    //here i would like to get the string array of Main1.java

    ((EditText)findViewById(R.id.editText1)).setText(kk);
}

can we get the string array values from Main1.java to Main2.java?


Put it into the starting intent:

Intent it = new Intent(Main1.this,Main2.class);
it.putExtra("MY_STRING_ARRAY", s2);

Get it back in the second activity:

String[] myStringArray = getIntent().getStringArrayExtra("MY_STRING_ARRAY");


If you want to send data from one activity to another then the best way would be send data using Intent object's putExtra method

Intent i = new Intent(Activity1.this, Activity2.class);
i.putExtra("data1", "some data");
i.putExtra("data2", "another data");
i.putExtra("data3", "more data");
startActivity(i);

and you can get the data from receiving activity Activity2 like this

Object data1 = getIntent().getExtras().get("data1");

Hope that helps


If you want to save your information via SharedPreference not just pass it along activities, use some code like this:

SharedPreferences settings = getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
    SharedPreferences.Editor prefEditor = settings.edit();
    prefEditor.putString("string_preference", "some_string");
    prefEditor.putInt("int_preference", 18);
    prefEditor.commit(); 

The commit command is the responsable of actually saving data to SharedPreferences.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号