save the data using shared preference in android

自闭症网瘾萝莉.ら 提交于 2020-01-02 06:55:29

问题


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.layout.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?


回答1:


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");



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/5973568/save-the-data-using-shared-preference-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!