简体   繁体   中英

Store editText value to another activity

I have more than 3 edittext. When i enter something inside edittext i need to save this to another screen using SharedPreferences. I used Intent before pass editText value to another Activity. But i need to save editText value later for editing purpose.

code:

Activity :

 et=(EditText)findViewById(R.id.et);

            et1=(EditText)findViewById(R.id.et1);

            btn=(Button)findViewById(R.id.btn);

    btn.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {

SharedPreferences preferences = getSharedPreferences("sample",0);
                  SharedPreferences.Editor editor = preferences.edit();

   String[] day_array = new String[] {et.getText().toString(), et1.getText().toString() };

                  editor.putInt("array_size", day_array.length);
                  for (int i = 0; i < day_array.length; i++)
                      editor.putString("array_" + i, day_array[i]);
                  editor.commit();


                  Intent intent = new Intent(Save.this, Get.class);
                  startActivity(intent);

Activity 1:

  SharedPreferences preferences = getSharedPreferences("sample",0);

  int size = preferences.getInt("array_size", 0);
      String[] Display_Room_array = new String[size];

      for (int i = 0; i < size; i++) {

          name = preferences.getString("array_" + i, Display_Room_array[i]);
          Display_Room_array[i] = name;

          txt=(TextView)findViewById(R.id.txt);
          txt.setText(Display_Room_array[i]);

          Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();

      }

Create a new class with Strings as static and save the text values there. Also put setter and getter functions in that class. In the next activity just create object of that class and get those values.

i didn't get what you want?

you said that you want to save 3 edit texts value in preferences ok?

you can do this in first activity too!

but if you are saying that you want to save edittext value in other activities then you should use intent.put extra to pass values to next activity and in that you can store values in shared preferences! what a big deal in that?

You can save some value in SharedPreferences and access that latter as follow...

et=(EditText)findViewById(R.id.et);

et1=(EditText)findViewById(R.id.et1);

btn=(Button)findViewById(R.id.btn);

btn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {

        Intent intent = new Intent(Save.this, Get.class);
            String[] myStrings = new String[] {txtt.getText().toString(),et.getText().toString() ,txtt1.getText().toString(),et1.getText().toString() };
                intent.putExtra("strings", myStrings);


                SharedPreferences preferences = getSharedPreferences("default", MODE_PRIVATE);
                  SharedPreferences.Editor editor = preferences.edit();
                  editor.putString("text1",txtt.getText().toString());
                  editor.putString("text2",et.getText().toString());
           .....
           ......   
                  editor.commit();


         startActivity(intent);

In next Activity you can access that preferences as follow..

.......
......

SharedPreferences preferences = getSharedPreferences("default", MODE_PRIVATE);

String text1= preference.getString("text1"," ");
String text2= preference.getString("text2"," ");
....
.....

txt.setText(text1);
txt2.setText(text2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM