Roma
Roma

Reputation: 19

preferences does not work?

I am using multiple EditTexts inside an Activity, rather than using sqlite, I'm saving the data unencrypted using SharedPreferences. However, as a beginner I do not know where I went wrong. When referred almost similar questions in stackoverflow, answers all looked different and added more chaos. Anyway here is my Java Code :

package labs.dc.cart.cart;

import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Switch;




public class MainActivity extends ActionBarActivity {

public static final String LOGTAG = "Cart";
public static final String SAVE = "saveText";
private SharedPreferences settings;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    settings = getPreferences(MODE_PRIVATE);


    final Switch mySwitch = (Switch)findViewById(R.id.switchButton);
    mySwitch.setChecked(false);

    mySwitch.setOnCheckedChangeListener
    (newCompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged
   (CompoundButton buttonView, boolean isChecked)
        {

            if(isChecked)
            {
                LinearLayout pop =  
                (LinearLayout)findViewById(R.id.linear);
                pop.setVisibility(View.VISIBLE);
            }

            else
            {

                LinearLayout pop = 
                (LinearLayout)findViewById(R.id.linear);
                pop.setVisibility(View.INVISIBLE);
            }

        }
    });



}




@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

    public void save(View v)
{
    Log.i(LOGTAG,"clicked set");
    SharedPreferences.Editor editor = settings.edit();
    EditText Et = (EditText)findViewById(R.id.editTextOne);
    String Prefvalue = Et.getText().toString();
    editor.putString(LOGTAG, Prefvalue);
    editor.commit();
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
} 

The corresponding EditText and button XML code is :

 <ImageButton
    android:id="@+id/saveButton"

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="save"/>

 <EditText
       android:id="@+id/editTextOne"
       android:inputType="text"
       android:layout_marginTop="110dp"
       android:background="#1ec0e9"
       android:alpha="0.5"
       android:ems="13"
       android:gravity="start"
       android:typeface="monospace"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />

Now, my question is why does the preference does not work in this case and which would be the best way to fix it?

Upvotes: 0

Views: 72

Answers (4)

Ram
Ram

Reputation: 1428

setting values in Preference:

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME,MODE_PRIVATE).edit();
editor.putString("nickname", "Alen");
editor.putInt("id", 12);
editor.commit();

Retrieve data from preference:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
String name = prefs.getString("nickname", "No name");
int idName = prefs.getInt("id", 0);
}

Upvotes: 0

Nagabhushan Baddi
Nagabhushan Baddi

Reputation: 1204

As far as I could understand your question, you are not retrieving the key-value pairs from the SharedPreferences. To retrieve the same use the following code in the onResume method:

    SharedPreferences settings = getPreferences();// no arguments/ parameters // since preferences are single in  number
Et.setText(settings.getString(LOGTAG));

Hope it helped! Good luck!

Upvotes: 0

K Guru
K Guru

Reputation: 1302

Use this code

SharedPreferences mSharedPreferences =getApplicationContext().getSharedPreferences("pref", MODE_PRIVATE);
        /*
         * TO WRITE VALUES INTO THE  PREFERENCE FILE
         */


        //get Editor object using  sharedPreference edit() Method

        Editor mEditor= mSharedPreferences.edit();
        //set boolean Values  into preference files

        mEditor.putBoolean("booleanValue", true);
        //set String value into the preference file
        mEditor.putString("stringValue", "hello");
        //set integer value into the preference file
        mEditor.putInt("intValue", 1);
        //set the long value into the preference file
        mEditor.putLong("longValue", 1212121);
        //set the float value into the preference file
        mEditor.putFloat("floatValue", 10.0f);
        //commit the changes
        mEditor.commit();


        /*
         * TO READ THE VALUES FROM THE PREFERENCE FILE
         */
        //get the boolean value from preference
        Boolean booleanValue=mSharedPreferences.getBoolean("boolean", true);
        //get the String value from  preference
        String stringValue=mSharedPreferences.getString("stringValue", "default value");
        //get the int value from preference
        int intValue=mSharedPreferences.getInt("intValue", 1);
        //get the long value from preference
        Long longValue=mSharedPreferences.getLong("longValue", 1212121);
        //get the float value from the preference
        Float floatValue=mSharedPreferences.getFloat("floatValue", 10.0f);

Detail

Upvotes: 0

user3686342
user3686342

Reputation:

Use this to store the values in preference

 SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);

 // Writing data to SharedPreferences
 Editor editor = settings.edit();
 editor.putString("key", "some value");
 editor.commit();

Upvotes: 1

Related Questions