2012-09-05 47 views
0

如何在意圖中獲得微調器選定位置並將其作爲參數傳遞給其他活動?如何在意圖中獲得選定的微調器值?

spinner sp; 
if (position==1) { 
do this 
} if (position==2) { 
do this 
} 

Intent i = new Intent(this.getApplicationContext(), AgAppMenu.class); 
Bundle bundle = new Bundle(); 
bundle.putString("mno", mobile.getText().toString()); 
bundle.putString("pinno", pin.getText().toString()); 
bundle.putLong("CODE",sp.getSelectedItemPosition()); 
+0

接受Answer.by滴答右記號吧。 – Harish

回答

0

如果我理解正確。現在,所有你缺少的是

intent.putExtras(bundle); 
startActivity(intent); 

編輯:

來獲取值,使用

(YourType) sp.getSelectedItem(); 

其中YourType是你希望得到的值的類型。如果它是Long,則將它投射到Long

+0

不,不,我沒有粘貼它,我想讓微調選擇值在這一行 bundle.putLong(「CODE」,sp.getSelectedItemPosition()); –

+0

此行sp.getSelectedItemPosition() 未獲得微調器選定值 –

+0

如果您嘗試獲取所選項目的值,請編輯您的問題以表明您正在查找VALUE,而不是POSITION。 –

0

定義一個類變量(這裏字符串selectedMonth),併爲其分配在下面的方法值,然後在意圖使用這個變量

公共無效onItemSelected(適配器視圖月,查看ARG1,INT ARG2,長ARG3){

selectedMonth= month.getItemAtPosition(arg2).toString(); 

     } 
+0

我不想給硬代碼價值我想給予動態價值意圖什麼是有價值的用戶選擇在spiner它的去作爲參數到另一個活動 –

1

也許這就是你想要做的?

sp.setOnItemSelectedListener(new OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> av, View v, 
        int pos, long id) { 
       //do something with the pos var 
      } 
      @Override 
      public void onNothingSelected(AdapterView<?> arg0) {}      
     }); 
+0

這隻顯示位置我想pas作爲參數spiner位置值 –

0
String item; 
sp.setOnItemSelectedListener(new OnItemSelectedListener() { 
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) { 
item = (String) arg0.getItemAtPosition(arg2); 
} 
public void onNothingSelected(AdapterView<?> arg0) { 
// TODO Auto-generated method stub 
} 
}); 

Intent i = new Intent(this.getApplicationContext(), AgAppMenu.class); 
Bundle bundle = new Bundle(); bundle.putString("CODE",item); 
+0

this如果我在微調器中添加4個位置,代碼將顯示選定的值? –

+0

非常感謝這個工作 –

+0

通過在它右邊打上正確的標記來接受答案。 – Harish

相關問題