I'm developing an app which adds two numbers. The user provides the numbers via EditText and the result is displayed via TextView. What I want to do is to save the values of the numbers entered by the user and the result via button (to see them whenever the user wants) and display them in the layout of another activity (whithout EditText's). Remark that the user would be able to see the results saved whenever he/she wants.
Hope you can help me. Thank you so much.
From Your question what i understand ::
Your first editText1
has value1
Your second editText2
has value2
textView1
has the result of value1
and value2
Solution :: Get the values from the views
and use intents
to pass the data between activities
Code ::
In your current Activity, create a new Intent:
- OnCreate()
Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("editText1",editText1.getText());
i.putExtra("editText2",editText2.getText());
i.putExtra("textView1",textView1.getText());
startActivity(i);
Then in the new Activity, retrieve those values:
- OnCreate()
Bundle extras = getIntent().getExtras();
if (extras != null) {
String editText1= extras.getString("editText1");
String editText2= extras.getString("editText2");
String textView1= extras.getString("textView1");
}
Use the variables editText1
, editText2
and textView1
in second activity to set the values to any views as u wish
HOPE THAT HELPS, let me know if you face any problems in debugging
make use of SharedPreferences
like this:
SharedPreferences sp= getSharedPreferences("ttt", 0);
SharedPreferences.Editor editor = sp.edit();
String etValue=(EditText)findViewById(R.id.yourEditTextId).getText().toString();
String tvValue=(TextView)findViewById(R.id.yourTextView).getText().toString();
editor.putString("etValue", etValue);
editor.putString("tvValue", tvValue);
editor.commit();
// in anywhere user wants
SharedPreferences settings = getSharedPreferences("ttt", 0);
String yourEditTextValue=settings.getString("etValie", "");
String yourTextViewValue=settings.getString("tvValie", "");
There is multiple ways to achieve that :
Method 1: Use static class setter and getter method:
create static class and set values from first activity and get value from second activity
Method 2:
Post your values through the intent
Method 3:
Use database to store data from one activity and get data from other activity
Method 4:
Use Shared preference
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.