1

I want to get the text from a EditText and Spinner at the same time, but when I choose a type other than All types, it changes to All types itself.

My question is why Feature Flim changes to All Types by itself after I click send Button, is not how to get selected item in spinner.

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"/>

here is the function sendMessage in MainActicity.java:

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);
}

and 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:

I choose feature flim

after I click send

after I click send

finally it shows:

enter image description here

Why it change to All types by itself? How to fix it?

3 Answers 3

2

You are setting the adapter and data within sendMessage. So, every time you click "Send", the Spinner is being repopulated, and thus ends up at position 0 again.

Instead, do the following:

First, place the following line in your class declaration:

private Spinner spinner = null;

Second, move these to onCreate() (or wherever you call 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) {
    }
});

Third and finally, leave the following in your sendMessage() method:

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);
Sign up to request clarification or add additional context in comments.

1 Comment

I would move EditText editText = (EditText) findViewById(R.id.edit_message); as a private field though, but this is just a matter of taste.
0

Try getting the text in another way:

spinner.geItemAtPosition(arg2).toString();

1 Comment

You mean in the public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)?
0

To get the selected item of Spinner do as below:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.