简体   繁体   English

在微调器中进行用户选择,将其存储在sharedPreferences中,并在其他活动中使用它

[英]Taking user selection in a spinner, storing it in sharedPreferences, and using it in another activity

I need the user to choose a restaurant, got a pretty large list with different selections. 我需要用户选择一家餐馆,并获得了很多不同选择的清单。 I then need to save that choice, preferably in something like sharedPreferences. 然后,我需要保存该选择,最好保存在诸如sharedPreferences之类的东西中。 And use it in another activity to display some data from an excel document. 并在另一个活动中使用它来显示excel文档中的一些数据。

currently my code looks like this: 目前我的代码如下所示:

in onCreate: 在onCreate中:

resturantSpinner = (Spinner) findViewById(R.id.resturantSpinner);
     // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.resturant_arrays, android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
   // Apply the adapter to the spinner
    resturantSpinner.setAdapter(adapter);

onItemSelected: onItemSelected:

public void onItemSelected(View view) {
      int userChoice = resturantSpinner.getSelectedItemPosition();
      SharedPreferences sharedPref = getSharedPreferences("resturantFile",0);
      SharedPreferences.Editor prefEditor = sharedPref.edit();
      prefEditor.putInt("userChoiceSpinner", userChoice);
      prefEditor.commit();
}

And retrieving the data, in another activity: 并在另一个活动中检索数据:

resturantTextView = (TextView) findViewById(R.id.resturantChoice);

    Intent intent = getIntent();

    SharedPreferences sharedPref = getSharedPreferences("resturantFile",MODE_PRIVATE);
    int resturantChoice = sharedPref.getInt("userChoiceSpinner",-1);

    resturantTextView.setText("" + resturantChoice);

I just use the textView to see what it saves like, and currently it just shows -1 我只是使用textView来查看其保存的内容,目前它只显示-1

edit: might as well just add this, userChoice value is 0. 编辑:最好只是添加它,userChoice值为0。

Try this: 尝试这个:

Add like this SharedPreferences sharedPref = getSharedPreferences("resturantFile",Activity.MODE_PRIVATE) instead of 像这样添加SharedPreferences sharedPref = getSharedPreferences("resturantFile",Activity.MODE_PRIVATE)而不是
SharedPreferences sharedPref = getSharedPreferences("resturantFile",0) . SharedPreferences sharedPref = getSharedPreferences("resturantFile",0)

For example: The way to save 例如:保存方式

SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("your_int_key", yourIntValue);
editor.commit();

The way to retrieve data in another activity: 在另一个活动中检索数据的方式:

SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
int myIntValue = sp.getInt("your_int_key", -1);

If that is not working for you then try this one: Change the default value -1 to 0, 如果那不适合您,请尝试以下一种方法:将默认值-1更改为0,

int myIntValue = sp.getInt("your_int_key", 0);

For more information use this question . 有关更多信息,请使用此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM