android - sharedPreference or onSaveInstanceState on an EditText/TextView result -


i have little test project below. want save edittext numbers entered , textview result (thing1, thing2, result) . what's best? onsaveinstancestate, sharedpreference, or different sqlite?

i've frustratingly tried first 2 (for embarrassingly long), couldn't figure out. please adding code below?

public class mainactivity extends actionbaractivity {    edittext thing1; edittext thing2; textview result;  double n1=0; double n2=0; double total=0;   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);       final button dividebutton = (button) findviewbyid(r.id.dividebutton);     dividebutton.setonclicklistener(new view.onclicklistener() {         public void onclick(view v) {              thing1 = (edittext) findviewbyid(r.id.thing1);             if (textutils.isempty(thing1.gettext().tostring())) {                 n1 = 0;}             else {                  n1= double.parsedouble(thing1.gettext().tostring());             }              thing2 = (edittext) findviewbyid(r.id.thing2);             if (textutils.isempty(thing2.gettext().tostring())) {                 n2 = 0;}             else {                 n2 = double.parsedouble(thing2.gettext().tostring());             }              if (n2 !=0){               total = (n1 / n2);}               final double total =  ((double)n1/(double)n2);              final textview result= (textview) findviewbyid(r.id.result);              string foo = string.format("%.2f", total);             result.settext(foo);           }      });       final button addbutton = (button) findviewbyid(r.id.addbutton);     addbutton.setonclicklistener(new view.onclicklistener() {         public void onclick(view v) {              thing1 = (edittext) findviewbyid(r.id.thing1);             if (textutils.isempty(thing1.gettext().tostring())) {                 n1 = 0;}             else {                 n1= double.parsedouble(thing1.gettext().tostring());             }              thing2 = (edittext) findviewbyid(r.id.thing2);             if (textutils.isempty(thing2.gettext().tostring())) {                 n2 = 0;}             else {                 n2 = double.parsedouble(thing2.gettext().tostring());             }               final double total =  (n1+n2);              final textview result= (textview) findviewbyid(r.id.result);              string foo = string.format("%.2f", total);             result.settext(foo);           }      });       final button subtractbutton = (button) findviewbyid(r.id.subtractbutton);     subtractbutton.setonclicklistener(new view.onclicklistener() {         public void onclick(view v) {              thing1 = (edittext) findviewbyid(r.id.thing1);             if (textutils.isempty(thing1.gettext().tostring())) {                 n1 = 0;}             else {                 n1= double.parsedouble(thing1.gettext().tostring());             }              thing2 = (edittext) findviewbyid(r.id.thing2);             if (textutils.isempty(thing2.gettext().tostring())) {                 n2 = 0;}             else {                 n2 = double.parsedouble(thing2.gettext().tostring());             }                 final double total =  (n1-n2);              final textview result= (textview) findviewbyid(r.id.result);              string foo = string.format("%.2f", total);             result.settext(foo);           }      });       final button multiplybutton = (button) findviewbyid(r.id.multiplybutton);     multiplybutton.setonclicklistener(new view.onclicklistener() {         public void onclick(view v) {              thing1 = (edittext) findviewbyid(r.id.thing1);             if (textutils.isempty(thing1.gettext().tostring())) {                 n1 = 0;}             else {                 n1= double.parsedouble(thing1.gettext().tostring());             }              thing2 = (edittext) findviewbyid(r.id.thing2);             if (textutils.isempty(thing2.gettext().tostring())) {                 n2 = 0;}             else {                 n2 = double.parsedouble(thing2.gettext().tostring());             }               final double total =  (n1*n2);              final textview result= (textview) findviewbyid(r.id.result);              string foo = string.format("%.2f", total);             result.settext(foo);            }      }); } 

if use values later when open application, can use sharedpreferences. based on code above can add code below save edittext data sharedpreferences , restore later.

to save edittext value:

sharedpreferences sharedpreferences = getapplication().getsharedpreferences("projectname", mode_private); sharedpreferences.editor editor = sharedpreferences.edit(); editor.putstring("thing1", thing1.gettext.tostring());     editor.commit(); 

to value sharedpreferences can in oncreate function:

sharedpreferences sharedpreferences = getapplication().getsharedpreferences("projectname", mode_private); string svalue = sharedpreferences.getstring("thing1", "default"); thing1.settext( svalue ); 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -