0

Functionality Description:

Image Button(calls on a secondary application) that allow users to enter and input text, handwritten as well as keyboard input. User will then have the option to save the text. When, user exit the secondary application, the image button is supposed to displayed the saved text.

Genuine Issue:

Have intended to use shared preferences method but the saved text is still not displayed. Can anyone please help. Following is the code used.

Code:

//EDITED VERSION TO CALL OUT THE SECONDARY APPLICATION FOR USER TO INPUT TEXTandr
private void addListenerOnButtonMyScript() {
    // TODO Auto-generated method stub
    imageButton = (ImageButton) findViewById(R.id.imageButton_myscript);

    imageButton.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {

            Log.i("SingleViewActivity:onCreate:addListenerOnButtonMyScript" + ":onKey" , "Initiate myScript:");

            final Intent intent = new Intent();
            intent.setClassName("com.visionobjects.textwidget.sample", "com.visionobjects.textwidget.sample.SampleActivity");
            startActivity(intent);
            SharedPreferences pref = getSharedPreferences(getString(R.string.pref_text),MODE_PRIVATE); 
            Log.i("Calling on Shared Preferences" , "Shared Preferences to pref_text:" + getString(R.string.pref_text) );
            SharedPreferences.Editor editor = pref.edit();
            editor.putString(getString(R.string.pref_text),"");
            editor.commit();
        }
    });
}
//EDITED VERSION TO CALL ON SHARED PREFERENCES FUNCTION TO DISPLAY EDITED TEXT FROM MY_SCRIPT-20/10/2014
private void loadSavedPreferences(){

    SharedPreferences pref = getSharedPreferences(getString(R.string.pref_text),MODE_PRIVATE);
    SharedPreferences.Editor editor= pref.edit();
    editor.commit();

}

Strings.xml

<resources>

<string name="app_name">SalesBase</string>
<string name ="pref_text">StandardPreferences</string>
<string name="contacts_default">Contacts Details\n名字:HARRY\n公司:BARTENDER\n路名:123, Road Name\nZip:Zip\nState:State\nMobile:012-345-6789\nOffice:12-345-678\n</string>
</resources>
ernst
  • 25
  • 6

1 Answers1

0

You are using wrong code to load the data. If loadSavedPreferences() is your final code to get data then you will not get any data. Try this

         private void loadSavedPreferences(){

         SharedPreferences pref = getSharedPreferences(getString(R.string.pref_text),MODE_PRIVATE);
         String data = prefs.getString(getString(R.string.pref_text), "");

   }

However, I would suggest you to use different keys for preference name and the data you are trying to save.

 SharedPreferences pref = getSharedPreferences(getString(R.string.pref_text),MODE_PRIVATE); 

        SharedPreferences.Editor editor = pref.edit();

Use a different key where you are saving the data in this line and obviously value is the data you want to save. Use something like this.

        editor.putString(key,value);
        editor.commit();

And when you want to show the data then use the same key to get the data.

     String data = prefs.getString(key, "");

Have a look at this as well. Android Shared preferences example

Community
  • 1
  • 1
Raheel
  • 181
  • 12
  • Thanks, I will try it out and if i have any further question, I would like to ask you further! – ernst Oct 23 '14 at 01:13