2012-12-05 252 views
1

我想同時從EditText和Spinner獲取文本,但是當我選擇除All類型之外的其他類型時,它將更改爲所有類型本身。使用按鈕在EditText和Spinner中獲取文本

我的問題是爲什麼Feature Flim在單擊發送按鈕後自行更改爲所有類型,不是如何在微調器中獲取所選項目。

的strings.xml:

<string-array name="country_arrays"> 
<item>All Types</item> 
<item>Feature Flim</item> 
<item>TV Series</item> 
<item>Video Games</item> 
</string-array> 

activity_main.xml中:

<Spinner 
    android:id="@+id/spinner1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:entries="@array/country_arrays" 
    /> 

<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/button_send" 
    android:onClick="sendMessage"/> 

這裏是MainActicity.java的函數SendMessage:

public void sendMessage(View view) { 

    final Spinner spinner = (Spinner) findViewById(R.id.spinner1); 
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
      R.array.country_arrays, android.R.layout.simple_spinner_item); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinner.setAdapter(adapter); 

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
      Toast.makeText(getBaseContext(), 
       "You have selected the book: " + String.valueOf(spinner.getSelectedItem()), 
       Toast.LENGTH_SHORT).show(); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> arg0) { 
     } 
    }); 

    Intent intent = new Intent(this, DisplayMessageActivity.class); 
    EditText editText = (EditText) findViewById(R.id.edit_message); 
    String message = editText.getText().toString() + String.valueOf(spinner.getSelectedItem()); 
    intent.putExtra(EXTRA_MESSAGE, message); 
    startActivity(intent); 
} 

和DisplayMessageActivity.java:

public class DisplayMessageActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Get the message from the intent 
    Intent intent = getIntent(); 
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 

    // Create the text view 
    TextView textView = new TextView(this); 
    textView.setTextSize(40); 
    textView.setText(message); 

    // Set the text view as the activity layout 
    setContentView(textView); 


} 

我選擇功能弗利姆:

I choose feature flim

我點擊發送後

after I click send

最後它表明:

enter image description here

爲什麼它自己更改爲所有類型?如何解決它?

回答

2

你內sendMessage設置適配器和數據。因此,每次點擊「發送」時,Spinner正在重新填充,因此再次結束於位置0。

相反,做到以下幾點:

首先,將下面的行放在類聲明:

private Spinner spinner = null; 

其次,將這些以onCreate()(或任何你叫setContentView()):

spinner = (Spinner) findViewById(R.id.spinner1); // Do not re-declare 
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
R.array.country_arrays, android.R.layout.simple_spinner_item); 
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
spinner.setAdapter(adapter); 

spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 
    @Override 
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
     Toast.makeText(getBaseContext(), 
      "You have selected the book: " + String.valueOf(spinner.getSelectedItem()), 
      Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onNothingSelected(AdapterView<?> arg0) { 
    } 
}); 

第三和最後,請在您的sendMessage()方法中留下以下內容:

Intent intent = new Intent(this, DisplayMessageActivity.class); 
EditText editText = (EditText) findViewById(R.id.edit_message); 
String message = editText.getText().toString() + String.valueOf(spinner.getSelectedItem()); 
intent.putExtra(EXTRA_MESSAGE, message); 
startActivity(intent); 
+0

我會移動 EditText editText =(EditText)findViewById(R.id.edit_message);雖然作爲私人領域,但這只是一個品味問題。 – Ivo

+0

謝謝!有用! – user23256

0

嘗試讓文本以另一種方式:

spinner.geItemAtPosition(arg2).toString(); 
+0

你的意思是在public void onItemSelected(AdapterView arg0,View arg1,int arg2,long arg3)? – user23256

+0

是的就在那裏! – Pavlos

0

爲了得到微調的所選項目做如下:

Spinner mySpinner=(Spinner)findViewById(R.id.Spinner_Id); 
mySpinner.getSelectedItem().toString();