取用户选择在一个离心器,其存储在共享prefences,而在另一个活动使用它 [英] Taking user selection in a spinner, storing it in sharedPrefences, and using it in another activity

查看:133
本文介绍了取用户选择在一个离心器,其存储在共享prefences,而在另一个活动使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的用户选择一间餐厅,获得了pretty大名单不同的选择。
那么我就需要pferably保存的选择,$ P $在类似的共享preferences。并用它在另一个活动从Excel文档显示的一些数据。

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. And use it in another activity to display some data from an excel document.

目前我的code是这样的:

currently my code looks like this:

在的onCreate:

in 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);

我只是用TextView的,看看有什么可以节省就好了,目前它只是显示-1

I just use the textView to see what it saves like, and currently it just shows -1

编辑:还不如干脆添加此,userChoice值为0

edit:might as well just add this, userChoice value is 0.

推荐答案

试试这个:

添加这样的共享preferences共享preF = getShared preferences(resturantFile,Activity.MODE_PRIVATE),而不是结果
共享preferences共享preF = getShared preferences(resturantFile,0)

Add like this SharedPreferences sharedPref = getSharedPreferences("resturantFile",Activity.MODE_PRIVATE) instead of
SharedPreferences sharedPref = getSharedPreferences("resturantFile",0).

例如:该方法来保存

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);

如果不工作的话,你试试这个:
默认值为-1更改为0,

If that is not working for you then try this one: Change the default value -1 to 0,

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

有关详细信息,使用此<一个href=\"http://stackoverflow.com/questions/14153286/not-getting-integer-value-from-shared$p$pference\">question.

For more information use this question.

这篇关于取用户选择在一个离心器,其存储在共享prefences,而在另一个活动使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆