2012-12-09 63 views
1

我有這樣的代碼微調選擇爲String

go_btn.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       Intent myIntent = new Intent(LoginActivity.this, WebActivity.class); 
       myIntent.putExtra("id", ed_txt.getText().toString()); 
       myIntent.putExtra("type", spinner.getText().toString()); 
       LoginActivity.this.startActivity(myIntent); 

我想在微調所選擇的選項來我的字符串。

這給我一個錯誤

myIntent.putExtra("type", spinner.getText().toString()); 

當我使用

myIntent.putExtra("type", spinner.getContext().toString()); 

我收到我的包名。

如何讓我的選擇項

回答

1

使用

instad的

myIntent.putExtra("type",spinner.getText().toString()); 

myIntent.putExtra("type", spinner.getSelectedItem().toString()); 

從微調

獲得所選項目的文本
+0

thanks mate!它現在正在工作。 – Georggroenendaal

+0

你必須接受答案,如果它適合你我的朋友... –

0

你將不得不增加一個監聽器到微調器來獲取選定的項目。它應該是這樣的

String selection = null; 
//Initialise Spinner (you may have done this already) 

Spinner spinner = (Spinner) findViewById(R.id.[your spinner id]); 

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 
      selection = parent.getItemAtPosition(pos).toString(); 
     } 
     public void onNothingSelected(AdapterView<?> parent) { 
     } 
    }) 

然後你可以把「選擇」字符串放到你的意圖。祝你好運:)

1

只要檢查該代碼,我希望這個代碼是有用的,

String getSelectItem = "null"; 

微調微調=(微調)findViewById(R.id. [您微調ID]);

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 
     getSelectItem = spinner.getSelectedItem().toString(); 
    } 
    public void onNothingSelected(AdapterView<?> parent) { 
    } 
});