1

I want to Save the User data in app as a One time activity so that it will not ask again and I have Given like this...

public class Main_A extends Activity {

public static final String Sh_Pref = "Sh_Pref_data";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.s_pref);


    SharedPreferences settings = getSharedPreferences(Sh_Pref, 0);
    HashMap<String, String> map = (HashMap<String, String>) settings.getAll();

    if (map != null && (map.containsKey("dtype") && map.containsKey("user_id") && map.containsKey("gname")&& map.get("dtype") != null && map.get("user_id") != null && map.get("gname") != null ))
    {
        Main_A.this.finish();

        Intent ss = new Intent(Main_A.this, Splash.class);
        ss.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        ss.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ss.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        ss.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        startActivity(ss);
    }

    else {

        Button b = (Button) findViewById(R.id.save);
        b.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Spinner dtype = (Spinner) findViewById(R.id.s1);
                EditText user_id = (EditText) findViewById(R.id.et1);
                Spinner gname = (Spinner) findViewById(R.id.s2);
                Spinner utype = (Spinner) findViewById(R.id.s3);
                Spinner ttype = (Spinner) findViewById(R.id.s4);

                if (dtype.getSelectedItem().toString().length() > 0 && user_id.getText().toString().length() > 0 && user_id.getText().toString().length() > 0 && utype.getSelectedItem().toString().length() > 0 && ttype.getSelectedItem().toString().length() > 0 && dtype.getSelectedItem().toString().length() > 0) {

                    String ct=dtype.getSelectedItem().toString();
                    String dom = user_id.getText().toString();
                    String am=gname.getSelectedItem().toString();
                    String ut=utype.getSelectedItem().toString();
                    String aname = getString(taname);
                    if(ut.equals("Talent"))
                    {
                        ut="T";
                    }
                    else if(ut.equals("A+"))
                    {
                        ut="A";
                    }
                    else if(ut.equals("Saint"))
                    {
                        ut="S";
                    }
                    String tt=ttype.getSelectedItem().toString();

                    Map<String, String> map = new HashMap<String, String>();

                    SharedPreferences settings = getSharedPreferences(Sh_Pref, 0);
                    SharedPreferences.Editor editor = settings.edit();

                    map.put("dtype",ct);
                    map.put("user_id", dom);
                    map.put("gname",am);

                    for (String key : map.keySet()) {
                        editor.putString(key, map.get(key));
                    }

                    editor.commit();

                    Main_A.this.finish();
                    Intent i = new Intent(Main_A.this, Splash.class);
                    startActivity(i);

                }

            }
        });

        Button c = (Button) findViewById(R.id.cancel);
        c.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Main_A.this);
                alertDialogBuilder.setTitle("ⓘ  Exit !  " +  getString(R.string.app_name));
                alertDialogBuilder
                        .setMessage(Html.fromHtml("<p style='text-align:center;'>Please Fill the required details</p><h3 style='text-align:center;'>Click Yes to Exit !</h4>"))
                        .setCancelable(false)
                        .setPositiveButton("Yes",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        moveTaskToBack(true);
                                        android.os.Process.killProcess(android.os.Process.myPid());
                                        System.exit(0);

                                    }
                                })

                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {

                                dialog.cancel();
                            }
                        });

                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();
            }
        });
    }
}


@Override
public void onBackPressed() {

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setTitle("ⓘ  Exit !  " +  getString(R.string.app_name));
    alertDialogBuilder
            .setMessage(Html.fromHtml("<p style='text-align:center;'>Please Fill the required details</p><h3 style='text-align:center;'>Click Yes to Exit !</h4>"))
            .setCancelable(false)
            .setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            moveTaskToBack(true);
                            android.os.Process.killProcess(android.os.Process.myPid());
                            System.exit(0);

                        }
                    })

            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    dialog.cancel();
                }
            });

    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();

}
}

Here Its saving all the Data and Running Successfully

Here I want to Edit the Same file in a different Activity.. But I need to View and what are previous values...

Because its a One time activity...After that app start...... So that If any thing is wrong Edit only that particular value...

Due to wrong details app may case errors.. for this user need to Clear cache/data or Uninstall and re install.. so To avoid This I want to add a Edit for Shared prefs..

Can any one suggest me on this kind

Don't Be negative
  • 1,215
  • 3
  • 19
  • 46

3 Answers3

1

Try this Here You can Edit/Update/Save Shared Preferences

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    private SharedPreferences prefs;
    private String prefName = "report";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText e_id=(EditText) findViewById(R.id.editText1);
        final EditText e_name=(EditText) findViewById(R.id.editText2);
        Button save=(Button) findViewById(R.id.button1);
        Button select=(Button) findViewById(R.id.button2);
        Button update=(Button) findViewById(R.id.button3);

        save.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            prefs = getSharedPreferences(prefName, MODE_PRIVATE);
                SharedPreferences.Editor editor = prefs.edit();

                //---save the values in the EditText view to preferences---
            editor.putInt("id", Integer.parseInt(e_id.getText().toString()));
                editor.putString("name", e_name.getText().toString());

                //---saves the values---
                    editor.commit();

                Toast.makeText(getBaseContext(), "Saved",
                Toast.LENGTH_SHORT).show();
        }
    });

        select.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            prefs = getSharedPreferences(prefName, MODE_PRIVATE);

                Toast.makeText(getBaseContext(), String.valueOf(prefs.getInt
                ("id", 22)), Toast.LENGTH_SHORT).show();
                Toast.makeText(getBaseContext(), prefs.getString
                ("name", "Sample Name"), Toast.LENGTH_SHORT).show();
        }
    });

        update.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            prefs = getSharedPreferences(prefName, MODE_PRIVATE);
                SharedPreferences.Editor editor = prefs.edit();

                //---save the values in the EditText view to preferences---
            editor.putInt("id", Integer.parseInt(e_id.getText().toString()));
                editor.putString("name", e_name.getText().toString());

                //---saves and update the values both are same---
                    editor.commit();

                Toast.makeText(getBaseContext(), "Updated Successfully",
                Toast.LENGTH_SHORT).show();
        }
    });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }    
}

Here You can Easily Update existing Value or Clear Exiting Value... Hence You already Done Saving Use this in your second Activity...

Whats Going On
  • 1,379
  • 6
  • 20
  • 49
0

Use this line to edit your shared prefs.. Put the key which you want to edit.

settings.edit().putString(key, map.get(key)).apply();
MUKUND U
  • 44
  • 6
  • Thanks @Mukund Udagani Sir......Can you Update Your answer... With my code...Actually I have Given Input code I want new one for Edit or Update with new code... I want to View and Edit... Or can you send me any example Links... – Don't Be negative Mar 10 '17 at 07:38
  • You can use this link, http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values – MUKUND U Mar 10 '17 at 10:23
0

Use following code to edit value for particular key in shared preference :

SharedPreferences.Editor editor = getSharedPreferences("Sh_Pref_data", MODE_PRIVATE).edit();
                                editor.putString("KEY",VALUE);
                                editor.commit();
R.R.M
  • 780
  • 4
  • 10
  • Can you suggest me where is the example.. So that I can create new one... – Don't Be negative Mar 10 '17 at 07:47
  • You can use it like this : SharedPreferences.Editor editor = getSharedPreferences("Sh_Pref_data", MODE_PRIVATE).edit(); editor.putString("user_id","12"); editor.commit(); – R.R.M Mar 10 '17 at 07:53