Android: How to make selected spinner item to Text

2019-09-06 15:12发布

问题:

I am trying to get the selected item from the spinner to be displayed DIRECTLY after it is selected. Imagine if this is like a quiz game. If you select one of the wrong ones, a TOAST will appear(which I cannot get working). If I select the right one, a different TOAST will appear and I will link it to a new activity. How can I get the onselect to output or display its selection to the textview and how can I implement an if statement for the right/wrong answers?

package com.example.test;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity implements OnItemSelectedListener{

String[] test_array;
TextView display, selected;
String result;
Spinner spinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.test_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
}

@Override
public void onItemSelected(final AdapterView<?> parent, View view, final int pos,
        long id) {
    // TODO Auto-generated method stub
    result = (String) spinner.getSelectedItem();
    display = (TextView) findViewById (R.id.display);
    display.setText(result);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}
}

I REVISED ACCORDING TO GOOGLE'S SAMPLES WITHIN THE SDK. HERE IS REVISED CODE.

package com.example.test;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity{

String[] test_array;
TextView display, selected;
String result, sSelect;
Spinner spinner;
int mPos;
String mSelection;
ArrayAdapter<CharSequence> mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    this.mAdapter = ArrayAdapter.createFromResource(this, R.array.test_array, android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(this.mAdapter);
    OnItemSelectedListener listener = new myOnItemSelectedListener();
    spinner.setOnItemSelectedListener(listener);
}

public class myOnItemSelectedListener implements OnItemSelectedListener {

    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int pos,
            long id) {
        // TODO Auto-generated method stub
        MainActivity.this.mPos = pos;
        MainActivity.this.mSelection = parent.getItemAtPosition(pos).toString();
        TextView resultText = (TextView) findViewById(R.id.display);
        resultText.setText(MainActivity.this.mSelection);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // TODO Auto-generated method stub

    }

}
}

The textview now changes to the selected item, now I will attempt the if/else. The if/else statement causes crash.

04-03 01:38:17.648: E/AndroidRuntime(817): FATAL EXCEPTION: main
04-03 01:38:17.648: E/AndroidRuntime(817): java.lang.NullPointerException
04-03 01:38:17.648: E/AndroidRuntime(817):  at com.example.test.MainActivity$myOnItemSelectedListener.onItemSelected(MainActivity.java:43)
04-03 01:38:17.648: E/AndroidRuntime(817):  at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
04-03 01:38:17.648: E/AndroidRuntime(817):  at android.widget.AdapterView.access$200(AdapterView.java:49)
04-03 01:38:17.648: E/AndroidRuntime(817):  at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860)
04-03 01:38:17.648: E/AndroidRuntime(817):  at android.os.Handler.handleCallback(Handler.java:615)
04-03 01:38:17.648: E/AndroidRuntime(817):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-03 01:38:17.648: E/AndroidRuntime(817):  at android.os.Looper.loop(Looper.java:137)
04-03 01:38:17.648: E/AndroidRuntime(817):  at android.app.ActivityThread.main(ActivityThread.java:4745)
04-03 01:38:17.648: E/AndroidRuntime(817):  at java.lang.reflect.Method.invokeNative(Native Method)
04-03 01:38:17.648: E/AndroidRuntime(817):  at java.lang.reflect.Method.invoke(Method.java:511)
04-03 01:38:17.648: E/AndroidRuntime(817):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-03 01:38:17.648: E/AndroidRuntime(817):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-03 01:38:17.648: E/AndroidRuntime(817):  at dalvik.system.NativeStart.main(Native Method)
04-03 01:39:04.718: E/Trace(865): error opening trace file: No such file or directory (2)
04-03 01:39:06.028: D/gralloc_goldfish(865): Emulator without GPU emulation detected.

Implemented code

public class myOnItemSelectedListener implements OnItemSelectedListener {

@Override
public void onItemSelected(AdapterView<?> parent, View v, int pos,
        long id) {
    // TODO Auto-generated method stub
    MainActivity.this.mPos = pos;
    MainActivity.this.mSelection = parent.getItemAtPosition(pos).toString();
    TextView resultText = (TextView) findViewById(R.id.display);
    resultText.setText(MainActivity.this.mSelection);
    if(result.equalsIgnoreCase("abc")){
    // Toast in here
     }else{
}
}

回答1:

try this

@Override
    public void onItemSelected(AdapterView<?>; parent, View view, int position, long id) {
        // On selecting a spinner item
        String item = parent.getItemAtPosition(position).toString();

        // Showing selected spinner item
        Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();

    }

try this for if-else statement

spinner = (Spinner) findViewById(R.id.test);

        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {

                String item = "nothing";
                 // On selecting a spinner item
                if(parent.getItemAtPosition(position).toString().equalsIgnoreCase("novels"))
                    item = parent.getItemAtPosition(position).toString();
                else{
                    //do other things
                }

                // showing a toast on selecting an item 
                Toast.makeText(parent.getContext(), item, Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }

        });


回答2:

as per your explanation:

@Override
public void onItemSelected(final AdapterView<?> parent, View view, final int pos,
        long id) {
    // TODO Auto-generated method stub
    result = spinner.getSelectedItem().toString();
    display = (TextView) findViewById (R.id.display);
    display.setText(result);
    if(result.equalsIgnoreCase("abc")){
        // for correct ans... open new activity using intent
    }else{
       // for wrong ans...
    }
}