Home > DeveloperSection > Forums > I am unable to retrieve the second value in Shared Preference
Manoj Bhatt
Manoj Bhatt

Total Post:153

Points:1079
Posted on    May-29-2013 11:44 PM

 Android Android 
Ratings:


 1 Reply(s)
 743  View(s)
Rate this:
Hi Everyone!

In my example, i am entering mobile no., message content in one activity. Before leaving that activity i am saving that info in "Shared Preference". In other activity i am trying to get those mob no,message,i am able to get no but unable to getting that message(second value).please help me to solve the issue.

DefaultDetails.java

package com.example.nirbhaya;

import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class DefaultDetails extends Activity implements OnClickListener{

    Button save,reset;
    EditText dMob,dMsg,dEmail;
    String defMobNo,defMsg,defEmail;
    SharedPreferences DefaultData;

    private static final String TAG = "DD-Activity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.defaultdetails);

        initializing();

    }
    private void initializing() {
        // TODO Auto-generated method stub


        save = (Button)findViewById(R.id.bsave1);
        reset = (Button)findViewById(R.id.bReset);

        dMob = (EditText)findViewById(R.id.etDefMobNo);
        dMsg = (EditText)findViewById(R.id.etDefMsg);
        dEmail = (EditText)findViewById(R.id.etDefEmail);

        save.setOnClickListener(this);
        reset.setOnClickListener(this);


    }
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch(arg0.getId())
        {
        case R.id.bsave1:
            defMobNo = dMob.getText().toString();
            defMsg = dMsg.getText().toString();
            defEmail = dEmail.getText().toString();
                Log.i(TAG,"DONE");
                DefaultData = getSharedPreferences("defMobileNo",0);
                SharedPreferences.Editor store = DefaultData.edit();
                store.putString("defMobileNo", defMobNo);
                store.putString("defMessgae", defMsg);
                store.putString("defEMail", defEmail);
                store.commit();
                Intent openStartingPoint = new Intent (getApplicationContext(), CurrentDetails.class);
                startActivity(openStartingPoint);
            break;
        case R.id.bReset:
            ((EditText) findViewById(R.id.etDefMobNo)).setText("");
            ((EditText) findViewById(R.id.etDefEmail)).setText("");
            ((EditText) findViewById(R.id.etDefMsg)).setText("");
            break;
        }
    }
}
DefSMS.java

package com.example.nirbhaya;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class DefSms extends Activity{

    Button buttonSend;
    String defNo,defMsg;
    SharedPreferences DefaultData;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.defsms);

        DefaultData = getSharedPreferences("defMobileNo",0);
        final String defNo = DefaultData.getString("defMobileNo","Couldn't load data");

        DefaultData = getSharedPreferences("defMessgae",0);
        final String defMsg = DefaultData.getString("defMessgae","Couldn't load data");

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

        buttonSend.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                try {
                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage(defNo, null, defMsg, null, null);
                    Toast.makeText(getApplicationContext(), "SMS Sent!",
                            Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(),
                            "SMS faild, please try again later!",
                            Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }

            }
        });

    }
}

Here def no getting message as Couldn't load data please help me

Thanks in advance! 


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    May-30-2013 5:30 AM

Hi Manoj!

Replace you line of code with this:

DefaultData = getSharedPreferences("defMobileNo",0);
final String defNo = DefaultData.getString("defMobileNo","Couldn't load data");
final String defMsg = DefaultData.getString("defMessgae","Couldn't load data");

It may be worked. 

Don't want to miss updates? Please click the below button!

Follow MindStick