How do I save information and make it show up on the screen for my Android app? -


i have following code android application, have written using sharedpreferences. got of information stackexchange, still unable values save appear on screen. here code:

package com.example.shivamgandhi.gyrosafe;  import android.content.intent; import android.os.bundle; import android.content.context; import android.support.design.widget.floatingactionbutton; import android.support.design.widget.snackbar; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; import android.view.view; import android.view.menu; import android.app.activity; import android.view.menuitem; import android.util.log; import android.widget.button; import android.content.sharedpreferences; import android.widget.edittext; import android.widget.toast; import android.widget.textview;  import org.w3c.dom.text;  public class settings_page_activity extends appcompatactivity implements view.onclicklistener{      edittext ed1, ed2;     button btnarray2[] = new button[5];     public static final string mypreferences = "myprefs";     public static final string phone = "phonekey";     public static final string email = "emailkey";     sharedpreferences sharedpreferences;     sharedpreferences.editor preferenceeditor;     private static final int preference_mode_private = 0;     textview textview;     textview textview2;      public void oncreate(bundle savedinstancestate){         super.oncreate(savedinstancestate);         setcontentview(r.layout.settings_page);         btnarray2[0] = (button)findviewbyid(r.id.button3);         btnarray2[1] = (button)findviewbyid(r.id.button4);         btnarray2[2] = (button)findviewbyid(r.id.button5);         btnarray2[3] = (button)findviewbyid(r.id.button6);         btnarray2[4] = (button)findviewbyid(r.id.button12);         (int = 0; <4; i++){             btnarray2[i].setonclicklistener(this);         }          sharedpreferences = this.getsharedpreferences(mypreferences, context.mode_private);          ed1 = (edittext)findviewbyid(r.id.edittext);         ed2 = (edittext)findviewbyid(r.id.edittext2);          textview = (textview)findviewbyid(r.id.textview41);         textview.settext(sharedpreferences.getstring("email", "no email"));          textview2 = (textview)findviewbyid(r.id.textview42);         textview2.settext(sharedpreferences.getstring("phone", "no phone"));     }      @override     public void onclick(view v) {         if(v == findviewbyid(r.id.button3)){             intent myintent = new intent(this, mainactivity.class );             startactivity(myintent);         }         else if(v == findviewbyid(r.id.button4)){             intent myintent = new intent(this, mainactivity.class);             startactivity(myintent);         }         else if(v == findviewbyid(r.id.button5)){             intent myintent = new intent(this, mainactivity.class);             startactivity(myintent);         }         else if(v == findviewbyid(r.id.button6)){             intent myintent = new intent(this, mainactivity.class);             startactivity(myintent);         }         else if(v == findviewbyid(r.id.button12)){             string e  = ed1.gettext().tostring();             string ph  = ed2.gettext().tostring();              sharedpreferences.editor editor = sharedpreferences.edit();              if(sharedpreferences.getstring("email", "no email") != "no email"){                 sharedpreferences.edit().remove("email").apply();             }              if(sharedpreferences.getstring("phone", "no phone") !=  "no phone"){                 sharedpreferences.edit().remove("phone").apply();             }              editor.putstring("email", e);             editor.putstring("phone", ph);             editor.commit();             toast.maketext(settings_page_activity.this,"thanks",toast.length_long).show();         }     } } 

this settings page application. want user able put in phone number , email address , save 2 values pressing button12. once these values saved, want email address , phone numbers appear on settings page whenever user comes page, done through textview , textview2.

if i've written isn't enough information, please let me know can clarify. in advanced :)

edit: have been asked r.layout.settings_page code. here is:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical" android:layout_width="match_parent"     android:layout_height="match_parent">      <relativelayout         android:layout_width="match_parent"         android:layout_height="match_parent">          <textview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:textappearance="?android:attr/textappearancelarge"             android:text="@string/settings_page"             android:id="@+id/textview"             android:layout_alignparenttop="true"             android:layout_centerhorizontal="true"             android:layout_margintop="21dp" />          <button             android:layout_width="fill_parent"             android:layout_height="wrap_content"             android:text="@string/begin_calibration"             android:id="@+id/button3"             android:layout_margintop="31dp"             android:layout_below="@+id/textview"             android:layout_alignparentstart="true" />          <button             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="@string/view_baseline_results"             android:id="@+id/button4"             android:layout_below="@+id/button3"             android:layout_alignparentstart="true" />          <button             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="@string/test_design_summary"             android:id="@+id/button5"             android:layout_aligntop="@+id/button4"             android:layout_alignparentend="true" />          <textview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:textappearance="?android:attr/textappearancemedium"             android:text="@string/email_results_to"             android:id="@+id/textview2"             android:layout_marginstart="24dp"             android:layout_below="@+id/button4"             android:layout_alignparentstart="true"             android:layout_margintop="40dp" />          <textview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:textappearance="?android:attr/textappearancemedium"             android:text="@string/emergency_number"             android:id="@+id/textview3"             android:layout_margintop="57dp"             android:layout_below="@+id/textview2"             android:layout_alignstart="@+id/textview2" />          <edittext             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:inputtype="textemailaddress"             android:ems="10"             android:id="@+id/edittext"             android:layout_below="@+id/textview2"             android:layout_alignstart="@+id/textview2" />          <edittext             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:inputtype="phone"             android:ems="10"             android:id="@+id/edittext2"             android:layout_below="@+id/textview3"             android:layout_alignstart="@+id/textview3" />          <button             style="?android:attr/buttonstylesmall"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="@string/home"             android:id="@+id/button6"             android:layout_alignparentbottom="true"             android:layout_toendof="@+id/textview"             android:layout_marginstart="26dp"             android:layout_marginbottom="41dp" />          <button             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="@string/save_information"             android:id="@+id/button12"             android:layout_above="@+id/button6"             android:layout_tostartof="@+id/button5" />          <textview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:id="@+id/textview42"             android:layout_gravity="right"             android:layout_above="@+id/edittext"             android:layout_alignend="@+id/edittext" />          <textview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:textappearance="?android:attr/textappearancemedium"             android:id="@+id/textview43"             android:layout_above="@+id/edittext2"             android:layout_toendof="@+id/button12" />      </relativelayout>      <textview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="new text"         android:id="@+id/textview41"         android:layout_gravity="right" /> </linearlayout> 

this looks classic race condition. you're mixing synchronous , asynchronous calls modifying shared preferences.

editor.commit(); synchronous meaning applied immediately, sharedpreferences.apply() performed in background, calls sharedpreferences.edit().remove("phone").apply(); , sharedpreferences.edit().remove("email").apply(); being executed after call editor.commit();

if doesn't fix issue, toast appear when hit button 12? there possibility equality check of v == findviewbyid(r.id.button12) failing, in case "preferred" way of doing check v.getid() == r.id.button12. it's uncommon, have heard of equality check failing when directly comparing views

edit

also, should noted call prefs.edit().remove().apply() after creating reference editor in code above (but use editor.stuff() later), redundant coding , should addressed, that's not germane actual issue

    btnarray2[0] = (button)findviewbyid(r.id.button3);     btnarray2[1] = (button)findviewbyid(r.id.button4);     btnarray2[2] = (button)findviewbyid(r.id.button5);     btnarray2[3] = (button)findviewbyid(r.id.button6);     btnarray2[4] = (button)findviewbyid(r.id.button12);     (int = 0; <4; i++){         btnarray2[i].setonclicklistener(this);     } 

your loop condition i<4 means setonclicklistener doesn't called btnarray2[4] meaning button12 never gets onclicklistener set. loop condition should

for (int = 0; < btnarray2.length; i++)


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -