bhanu kaushik
bhanu kaushik

Reputation: 391

Android Display text from spinner to Edit Text view and save in Shared Preferences

I have placed a spinner in my application, i want to display 5 sentences/strings from which when i choose one of them, it gets displayed to the edit text view and gets saved to the Shared Preference simultaneously and i am even allowing user to enter any text that he wants if he do not want to choose any of the provided strings. The strings are fixed and will not be changed.I am able to save the text message that user is providing of its own but I am facing problem in displaying the strings on to the Edit Text View and saving itin Shared Preference. Please help me out

EditText message;
Spinner spinner;
String[] alert = { "I am in danger", "Help Me", "Watch Out", "Look For Me",
            "Cover ME" };
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner = (Spinner) findViewById(R.id.Spin_alert_message);

         spinner = (Spinner)findViewById(R.id.Spin_alert_message);
     ArrayAdapter<String> adp = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, alert);
     spinner.setAdapter(adp);



        // for saving text that user can change as per need
        final SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(this);

        message = (EditText) findViewById(R.id.et_message);
        // loads the text that has been stored to SP and set it to Edit Text
        message.setText(preferences.getString("autoSave", ""));
        // adding addTextChangedListner() to the Edit Text View
        message.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                // saving text after it is changed by the user
                preferences.edit().putString("autoSave", s.toString()).commit();

            }
        });

Upvotes: 0

Views: 838

Answers (1)

Bandreid
Bandreid

Reputation: 2733

Well, first of all message.setText(pos); will set write in the EditText the selected position not the selected text from the Spinner. In order to fix this use:

spinner = (Spinner)findViewById(R.id.Spin_alert_message);
ArrayAdapter<String> adp = new ArrayAdapter<String>(getApplicationContext(),
    android.R.layout.simple_spinner_item, alert);
spinner.setAdapter(adp);

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent,
        View view, int position, long id) {

        String text = (String) parent.getAdapter().getItem(position);
        message.setText(pos);
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // We do... nothing! :)
    }
});

Secondly, I am not sure if your solution with the TextWatcher is the best one for saving the text in SharedPreferences. But if it works keep it like that.

Your question is kind of ambigous so I don't know if this answer solves all your problems. In case you need more help please try to describe the issue in more detail.

Upvotes: 1

Related Questions