0

我试图从微调器中获取选定的项目,以便在选择后直接显示。想象一下,如果这就像一个问答游戏。如果您选择了其中一个错误,则会出现一个 TOAST(我无法正常工作)。如果我选择了正确的,则会出现一个不同的 TOAST,我会将它链接到一个新活动。如何让 onselect 输出或将其选择显示到 textview 以及如何为正确/错误答案实现 if 语句?

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

}
}

我根据 SDK 中的 GOOGLE 示例进行了修改。这是修改后的代码。

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

    }

}
}

textview 现在更改为所选项目,现在我将尝试 if/else。 if/else 语句会导致崩溃。

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.

实现代码

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{
}
}
4

2 回答 2

3

试试这个

@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();

    }

试试这个if-else声明

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

            }

        });
于 2013-04-02T08:31:59.557 回答
0

根据你的解释:

@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...
    }
}
于 2013-04-02T08:39:11.687 回答