2016-04-28 65 views
-1

如何顯示微調器中的選定項目微調控制器B中的顯示?在微調器中獲取值顯示

<string-array name="type_report"> 
    <item>Emergency</item> 
    <item>Sponsor</item> 
    <item>House</item> 
</string-array> 

兩個旋轉器使用相同的「type_report」..spinner A和B將顯示在第一個位置的緊急情況。我的問題是,當我在家裏的活動飛旋一個選擇「豪斯醫生」,然後在其他活動微調B就在第一個位置顯示「豪斯醫生」 ..

微調名單

<item>Emergency</item>  
    <item>Sponsor</item>  
    <item>House</item> 

後選擇「
微調B文件清單將顯示

<item>House</item> 
<item>Emergency</item> 
<item>Sponsor</item> 
+0

表現出一定的代碼/ –

+0

使用的setSelected方法,例如第一個活動然後找到該位置並將其傳遞給第二個活動並使用setselected(position)方法設置默認微調器項目 – USKMobility

+0

在微調器A上使用'setOnItemSelectedListener'並在A被更改時更改B –

回答

1
spinnerA.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
    /** 
    * Called when a new item is selected (in the Spinner) 
    */ 
    public void onItemSelected(AdapterView<?> parent, View view, 
       int pos, long id) { 
     Intent intent=new Intent(this,SecondaActivity.class); 
     intent.putExtra("index",pos); 
     startActivity(intent); 


     } 

     public void onNothingSelected(AdapterView<?> parent) { 
      // Do nothing, just another required interface callback 
     } 

}); 

現在您的secondActivity你必須把所選擇的指數的項目在第一位置,這樣

Intent mIntent = getIntent(); 
int pos= mIntent.getIntExtra("index", 0); 
String valueAtIndex = yourArray[pos]; 
    for(int i = pos; i > 0; i--){ 
    yourArray[i] = yourArray[i-1]; 
    } 
    yourArray[0] = valueAtIndex; 
//now set this array to second Spinner 
    ArrayAdapter spinnerBArrayAdapter = new ArrayAdapter(this, 
    android.R.layout.simple_spinner_dropdown_item, 
    yourArray); 
    spinnerB.setAdapter(spinnerArrayAdapter); 

測試,如果房子是在選擇的工作代碼

+0

實際上,當我在微調器A中選擇項目後,我將其他活動包含在微調器B中,因此我希望顯示項目在微調器B的第一個位置選擇。 –

+0

對不起,您的意思是'yourArray'..?那個字符串數組「type_report」? –

+0

是的,它是你的字符串數組。 –