gpt4 book ai didi

android - 相关微调器 Android

转载 作者:行者123 更新时间:2023-11-30 04:36:32 26 4
gpt4 key购买 nike

我是 android 初学者。我正在做一个项目。但是我很难做两个相互关联的微调器。实际上,一个微调器用于乡村,另一个用于城市。第二个微调器将显示城市,而不是所选的国家/地区。

我使用了“OnItemSelectedListener”,但不能在 OnItemSelectedListener 中使用“ArrayAdapter.createFromResource e”。我尝试了很多其他方法,但仍然没有用。有人可以帮帮我吗???

(附:我已经阅读并尝试了有关此主题的其他帖子,但仍然无效)这是代码:

spinner.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {


int spinnerId = spinner.getSelectedItemPosition();
if (spinnerId==0){
adaptert = ArrayAdapter.createFromResource(
this, R.array.tirana, android.R.layout.simple_spinner_item);
adaptert.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
else if (spinnerId==1) {
adaptert = ArrayAdapter.createFromResource(
this, R.array.durres, android.R.layout.simple_spinner_item);
adaptert.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}

spinnert.setAdapter(adaptert);


}

public void onNothingSelected(AdapterView<?> parent) {

}
});

最佳答案

public class AdapterViewImplementation extends Activity implements OnItemSelectedListener{

Spinner sp1; // One Spinner
Spinner sp2; // Another Spinner
ArrayAdapter stateAdapter; // Adapter for state
ArrayAdapter cityAdapter; // Adapter for city

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

sp1 = (Spinner)findViewById(R.id.Spinner01);
sp2 = (Spinner)findViewById(R.id.Spinner02);


stateAdapter = ArrayAdapter.createFromResource(AdapterViewImplementation.this,
R.array.state, android.R.layout.simple_spinner_item);
stateAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(stateAdapter);
sp1.setOnItemSelectedListener(AdapterViewImplementation.this);


cityAdapter = ArrayAdapter.createFromResource(AdapterViewImplementation.this,
R.array.city, android.R.layout.simple_spinner_item);
cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(cityAdapter);
sp2.setOnItemSelectedListener(AdapterViewImplementation.this);


}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub

if(arg0 == sp1){

sp2.setSelection(arg2);

}else{

sp1.setSelection(arg2);

}

}

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

}
}

关于android - 相关微调器 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6733627/

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