2014-01-12 58 views
0

我有一个微调器,它取3个值("Birth day","Marriage","Anniversary")在android中设置微调器值

我想将选定的微调值从一个活动传递给另一个活动,并在不同活动的微调器中设置该值。

我能够使用Intent传递微调器的值,但不知道如何在第二个活动中设置该值。

回答

0

在第二个活动中创建相同的字符串数组,并在第一个使用意向的活动中传递在微调器中选择了哪个位置。然后你可以使用下面的代码在第二活动设置微调值

spinner.setSelection(getIntent().getIntExtra("position",0);); 
0

------------- CurrentActivity ------

Intent i = new Intent(CurrentActivity.this,NextActivity.class); 

Bundle b = new Bundle(); 

b.putString("name", sp.getSelectedItem().toString()); 

i.putExtras(b); 

startActivity(i); 

- ------------ NextActivity ------------------

Bundle b = this.getIntent().getExtras(); 

String name = b.getString("name"); 

((TextView)findViewById(R.id.textView1)).setText(name); 
0

在第一次使用Spinner项目时,选择SharedPreference

public void onItemSelected(AdapterView<?> parent, View view, 
        int position, long id) { 
       int Position=position;//it will have selected item position 
       SharedPreferences sp= getSharedPreferences("settings",MODE_PRIVATE); 

       SharedPreferences.Editor editor=sp.edit(); 
       editor.putInt("Position", Position); 
       editor.commit(); 

这将保存您在设置中的位置。 现在,在您的下一个活动做一个功能

int getPos(String str) 
    { 
    SharedPreferences sp=getSharedPreferences("settings",MODE_PRIVATE); 
    int Position=sp.getInt(str,0); 
    return Position; 
    } 

和设置适配器后微调加上这样一句话:

Spinner.setSelection(getPos("Position")); 
    //your code