XML : preferences does not work?


I am using multiple EditTexts inside an Activity, rather than using sqlite, I'm saving the data unencrypted using preferences. 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?

No comments:

Post a Comment