glassraven
glassraven

Reputation: 323

Android: saving and reading data from user's input?

I want to prompt the user his name in an AlertDialogue, save his name on Shared Prefferences and set his name in a text view so when he returns he can see his name. So far I have the dialogue input, but i'm not sure if it is saving because the text view shows something like "Android Widget EditText(9o179...etc)" What am I doing wrong? Code:

String name; //global
TextView user;

Oncreate:

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

    buttonCreate();
    preferences();
    togglePlay();
}

TogglePlay Method:

public void togglePlay() {
    init.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if (isChecked) {
                KeysOn();

            } else {

                KeysOff();
            }
        }
    });
}

public void KeysOn() {
   Toast.makeText(getApplicationContext(), getString(R.string.ToastToggleOn), Toast.LENGTH_SHORT).show();
    user.setText(name);
   //etc
}

The preferences method called in the onCreate:

 public void preferences(){


    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());


     name= settings.getString("name",name);
     saveName();

    data = settings.getString("stage", "Indoors");
    String backGround = settings.getString("stage", "Indoors");

    if (backGround.equals("Indoors")) {
        Picasso.with(this).load(R.drawable.shocked_crowd).fit().centerCrop().into(palco);
        Toast.makeText(getApplicationContext(), getString(R.string.stageIndoors), Toast.LENGTH_LONG).show();


    }
    if (backGround.equals("Street")) {
        Picasso.with(this).load(R.drawable.coins).fit().centerCrop().into(palco);
        Toast.makeText(getApplicationContext(), getString(R.string.stageStreet), Toast.LENGTH_LONG).show();
    }
}

EDIT: missing code:

public void popUp(){

    AlertDialog.Builder questionName = new AlertDialog.Builder(this);

    questionName.setTitle(" Enter your name ");
    EditText input=new EditText(this);
    name=input.toString();
    LinearLayout.LayoutParams lp= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    input.setLayoutParams(lp);
    questionName.setView(input);
    questionName.setNegativeButton(" cancel ", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int id){

        }
    });

    questionName.setPositiveButton(" done ", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int id){

        }
    });

    AlertDialog dialog = questionName.create();

    dialog.show();
}
public void saveName(){
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("name", name);
    editor.commit();
}

Upvotes: 1

Views: 195

Answers (2)

Khaledonian
Khaledonian

Reputation: 2203

You're recommended to visit Android Studio Storage Option to have an enlarged perception of the mechanism and functionality

that being said, allow me to provide you with a snippet of how I store values in my application

Note you need to make minor adjustments, since I am applying Sharedpreferences inside a fragment

first

   addusername = (EditText) oView.findViewById(R.id.addnewUsername);

second

  //adjustment and reattachment
  String bonsiour = addusername.getText().toString().trim();

            SharedPreferences sPref = getActivity().getPreferences(0);
            SharedPreferences.Editor edt = sPref.edit();
            edt.putString("key1", bonsiour);
            edt.commit();


            //toast to confirm value has been saved
            String buzo = sPref.getString("key1", "empty");
            Toast.makeText(getActivity(), "You're  " + buzo + "!!", Toast.LENGTH_LONG).show();

This is how to extract/read from it

  SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
    String name = prefs.getString("key1", "No name defined");

    if(name.equals("PDF_FOUND")){
        Toast.makeText(Controller.this,"IT WORKED !", Toast.LENGTH_SHORT).show();




    } else{
       Toast.makeText(Controller.this,"BAD NEWS !", Toast.LENGTH_SHORT).show();

 }    
}

Upvotes: 1

Shijil
Shijil

Reputation: 2246

use edittext.getText().toString(); to get text from EditText,

public void popUp(){

    AlertDialog.Builder questionName = new AlertDialog.Builder(this);

    questionName.setTitle(" Enter your name ");
    EditText input=new EditText(this);

    LinearLayout.LayoutParams lp= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    input.setLayoutParams(lp);
    questionName.setView(input);
    questionName.setNegativeButton(" cancel ", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int id){

        }
    });

    questionName.setPositiveButton(" done ", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int id){
             name=input.getText().toString(); // see this line. 
             saveName();
        }
    });

    AlertDialog dialog = questionName.create();

    dialog.show();
}

Upvotes: 0

Related Questions