gpt4 book ai didi

java - Android 使用一个微调器和两个适配器

转载 作者:行者123 更新时间:2023-11-29 21:16:18 26 4
gpt4 key购买 nike

我有一个微调器、两个适配器和两个按钮

当第一个按钮被点击时,maleAdapter 被调用单击另一个按钮时,将调用 femaleAdapter。到目前为止,这是完美的。

现在。我的问题是当调用每个适配器时调用两个不同的数组,显然,但我希望它们做不同的事情。

Spinner spinner;
String[] maleItems = { "item 1", "item 2", "item 3" };
String[] femaleItems = { "item 3", "item 2", "item 1", "item 0" };

Button maleButton;
Button femaleButton;

ArrayAdapter<String> maleAdapter;
ArrayAdapter<String> femaleAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final ArrayAdapter<String> maleAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, maleItems);
final ArrayAdapter<String> femaleAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, femaleItems);

spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(maleAdapter);
spinner.setOnItemSelectedListener(this);

// Buttons
maleButton = (Button) findViewById(R.id.maleButton);
femaleButton = (Button) findViewById(R.id.femaleButton);

maleButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
spinner.setAdapter(maleAdapter);

}
});

femaleButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
spinner.setAdapter(femaleAdapter);

}
});

}

在我的 onItemSelected 上,我想根据按下的按钮更改发生的情况。

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {

int position = spinner.getSelectedItemPosition();
SpinnerAdapter adap = spinner.getAdapter();

if (adap.equals(maleAdapter)) {
switch (position) {
case 0:
// do something only the maleAdapter does
break;
case 1:
// do something only the maleAdapter does
break;
case 2:
// do something only the maleAdapter does
break;
}
} else if (adap.equals(femaleAdapter)) {
switch (position) {
case 0:
// do something only the femaleAdapter does
break;
case 1:
// do something only the femaleAdapter does
break;
case 2:
// do something only the femaleAdapter does
break;
}
}
}

我的 onClick 方法运行完美,但适配器更改给我一个错误,我不知道为什么。

错误

01-25 04:11:18.873: E/AndroidRuntime(21871): FATAL EXCEPTION: main
01-25 04:11:18.873: E/AndroidRuntime(21871): java.lang.NullPointerException
01-25 04:11:18.873: E/AndroidRuntime(21871): at com.MainActivity.onItemSelected(MainActivity.java:117)
01-25 04:11:18.873: E/AndroidRuntime(21871): at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
01-25 04:11:18.873: E/AndroidRuntime(21871): at android.widget.AdapterView.access$200(AdapterView.java:42)
01-25 04:11:18.873: E/AndroidRuntime(21871): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
01-25 04:11:18.873: E/AndroidRuntime(21871): at android.os.Handler.handleCallback(Handler.java:587)
01-25 04:11:18.873: E/AndroidRuntime(21871): at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 04:11:18.873: E/AndroidRuntime(21871): at android.os.Looper.loop(Looper.java:130)
01-25 04:11:18.873: E/AndroidRuntime(21871): at android.app.ActivityThread.main(ActivityThread.java:3691)
01-25 04:11:18.873: E/AndroidRuntime(21871): at java.lang.reflect.Method.invokeNative(Native Method)
01-25 04:11:18.873: E/AndroidRuntime(21871): at java.lang.reflect.Method.invoke(Method.java:507)
01-25 04:11:18.873: E/AndroidRuntime(21871): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
01-25 04:11:18.873: E/AndroidRuntime(21871): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
01-25 04:11:18.873: E/AndroidRuntime(21871): at dalvik.system.NativeStart.main(Native Method)

最佳答案

final ArrayAdapter<String> maleAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, maleItems); 

它在 onCreate 的本地范围内,删除它并在那里初始化全局 maleAdpter

只需删除 maleAdapter 和 femaleAdapter 之前的最后一个 ArrayAdapter。

关于java - Android 使用一个微调器和两个适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21348902/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com