1

我想同时从 EditText 和 Spinner 获取文本,但是当我选择所有类型以外的类型时,它本身会更改为所有类型。

我的问题是为什么在我单击发送按钮后 Feature Flim 会自行更改为所有类型,而不是如何在微调器中获取选定项目。

字符串.xml:

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

活动主.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);


}

我选择长片:

我选择故事片

在我点击发送后

在我点击发送后

最后它显示:

在此处输入图像描述

为什么它会自行更改为所有类型?如何解决?

4

3 回答 3

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);
于 2012-12-06T00:30:30.150 回答
0

尝试以另一种方式获取文本:

spinner.geItemAtPosition(arg2).toString();
于 2012-12-06T00:16:41.537 回答
0

要获取 Spinner 的选定项目,请执行以下操作:

Spinner mySpinner=(Spinner)findViewById(R.id.Spinner_Id);
mySpinner.getSelectedItem().toString();
于 2012-12-06T00:18:35.790 回答