Android Save Spinner Selection

2019-08-28 01:48发布

I have two activities. In the second activity I have a spinner. what I would like to happen is after the user selects an item from the spinner it will save via actionbar press and on back press which will load the previous activity. Based on my research my activity is supposed to look something like the following below but it's not working what am I doing wrong??

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    spin = (Spinner)findViewById(R.id.editspin);

    Intent i = this.getIntent();
    note = new ArgueItem();
    note.setKey(i.getStringExtra("key"));
    note.setText(i.getStringExtra("text"));

    EditText et = (EditText)findViewById(R.id.argueEdit);
    et.setText(note.getText());
    et.setSelection(note.getText().length());


}private boolean saveState() {
          prefs = PreferenceManager.getDefaultSharedPreferences(this);
          SharedPreferences.Editor prefEditor = prefs.edit();
          int daddy = spin.getSelectedItemPosition();
          prefEditor.putInt("savedValue",daddy);
          prefEditor.commit();
        return true;
    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    EditText et = (EditText)findViewById(R.id.argueEdit);
    String argueText = et.getText().toString();
    if(argueText.equals("")){
        Toast.makeText(this, "Please Enter A New ", Toast.LENGTH_SHORT).show();

        return false;
    }
    if (item.getItemId() == android.R.id.home) {
        saveAndFinish();
    }
    return false;
}
@Override
public void onBackPressed() {
    EditText et = (EditText)findViewById(R.id.argueEdit);
    String argueText = et.getText().toString();
    if(argueText.equals("")){
        Toast.makeText(this, "Please Enter A New ", Toast.LENGTH_SHORT).show();

        return;
    }else{
    saveAndFinish();
}

3条回答
【Aperson】
2楼-- · 2019-08-28 02:24

In your second activity, you have to override the onPause() and. Inside it write the saving process.

protected void onPause(){
    super.onPause();

    //Include the code which, save the data.
}
查看更多
祖国的老花朵
3楼-- · 2019-08-28 02:31

This is how i'm initializing my spinner which is in the ActionBar. I'm not adding it as a custom view, but I'm using the drop down menu feature.

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setListNavigationCallbacks(adapter, new ActionBar.OnNavigationListener() {
        @Override
        public boolean onNavigationItemSelected(int itemPosition, long itemId) {

            //save in preferences
            PreferenceManager.getDefaultSharedPreferences(MainActivity.this).edit().
                    putInt(SELECTED_DIARY_PREF, itemPosition).commit();

            return true;
        }
    });
int selPosition = PreferenceManager.getDefaultSharedPreferences(this).getInt(SELECTED_DIARY_PREF, 0);
actionBar.setSelectedNavigationItem(selPosition);

What this code does is: saving the preference when an item of the menu is clicked, and restoring that preference when the activity is launched. Hope it helps.

查看更多
小情绪 Triste *
4楼-- · 2019-08-28 02:39

You should use a FragmentActivity and add/remove fragments within the same activity. check these resources: http://developer.android.com/guide/components/fragments.html http://www.vogella.com/articles/AndroidFragments/article.html

查看更多
登录 后发表回答