我试图让微调器中的选定项目在选中后直接显示。想象一下,如果这就像一个问答游戏。如果您选择了其中一个错误,将出现一个TOAST(我无法工作)。如果我选择了正确的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{
}
}
答案 0 :(得分: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
}
});
答案 1 :(得分: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...
}
}