在片段中工作时,我试图返回通过在微调器中选择项目而确定的值,但我无法使其工作。方法中的Toast通知甚至不起作用,似乎问题在方法内。我该如何解决这个问题?
public Spinner districtSpinner, countySpinner;
public int selectedDistrict;
是使用的变量。这是我的父级微调器方法,zoneSpinner()
public void districtSpinner() {
ArrayAdapter<String> Dadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.districtNames));
Dadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
districtSpinner.setAdapter(Dadapter);
districtSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
selectedDistrictPosition = districtSpinner.getSelectedItemPosition();
switch (selectedDistrictPosition) {
case 0: {
selectedDistrict = 1;
break;
}
case 1: {
selectedDistrict=2;
break;
}
case 2: {
selectedDistrict=3;
break;
}
case 3: {
selectedDistrict=4;
break;
}
case 4: {
selectedDistrict=5;
break;
}
case 5: {
selectedDistrict=6;
break;
}
case 6: {
selectedDistrict=8;
break;
}
case 7: {
selectedDistrict=9;
break;
}
case 8: {
selectedDistrict=10;
break;
}
case 9: {
selectedDistrict=11;
break;
}
case 10: {
selectedDistrict=12;
break;
}
default: {
selectedDistrict=1;
break;
}
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
}
这是儿童旋转器&#34; countySpinner&#34;我试图依赖父级微调器。该方法应该根据selectedDistrict进行切换,并使用包含每个区域的县(R.array.districtx)的相应字符串数组设置微调器。目前第二个微调器没有做任何事情,就我所关心的那样,县甚至没有被调用过。
public void countySpinner() {
switch (selectedDistrict) {
case 0: {
break;
}
case 1: {
ArrayAdapter<String> Cadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.district1));
Cadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
countySpinner.setAdapter(Cadapter);
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
break;
}
case 2: {
ArrayAdapter<String> Cadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.district2));
Cadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
countySpinner.setAdapter(Cadapter);
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
break;
}
case 3: {
ArrayAdapter<String> Cadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.district3));
Cadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
countySpinner.setAdapter(Cadapter);
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
break;
}
case 4: {
ArrayAdapter<String> Cadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.district4));
Cadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
countySpinner.setAdapter(Cadapter);
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
break;
}
case 5: {
ArrayAdapter<String> Cadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.district5));
Cadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
countySpinner.setAdapter(Cadapter);
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
break;
}
case 6: {
ArrayAdapter<String> Cadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.district6));
Cadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
countySpinner.setAdapter(Cadapter);
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
break;
}
case 7: {
break;
}
case 8: {
ArrayAdapter<String> Cadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.district8));
Cadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
countySpinner.setAdapter(Cadapter);
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
break;
}
case 9: {
ArrayAdapter<String> Cadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.district9));
Cadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
countySpinner.setAdapter(Cadapter);
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
break;
}
case 10: {
ArrayAdapter<String> Cadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.district10));
Cadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
countySpinner.setAdapter(Cadapter);
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
break;
}
case 11: {
ArrayAdapter<String> Cadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.district11));
Cadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
countySpinner.setAdapter(Cadapter);
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
break;
}
case 12: {
ArrayAdapter<String> Cadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.district12));
Cadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
countySpinner.setAdapter(Cadapter);
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
break;
}
default: {
break;
}
}
答案 0 :(得分:1)
您可以像I want to setup my second spinner according to the choice made in first spinner
更好地说明问题。然后你最好这样做,因为你可以使用相同的代码。
public void districtSpinner() {
ArrayAdapter<String> Dadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.districtNames));
Dadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
districtSpinner.setAdapter(Dadapter);
districtSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int selectedDistrict = position+1;
// do yourself a favor and intoduce an empty choice for 7;
countySpinner(selectedDistrict);
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
}
private int getDistrictResourceId ( int districtnr ) {
int resId = R.array.district1;
switch ( districtnr )
{
case 1:
resId = R.array.district1;
break;
case 2:
resId = R.array.district2;
break;
// please add the rest
}
return resId;
}
public void countySpinner( int districtnr) {
int resId = getDistrictResourceId( districtnr );
ArrayAdapter<String> Cadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(resId));
Cadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
countySpinner.setAdapter(Cadapter);
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
}
}
答案 1 :(得分:0)
就我所见,你应该看到Toast。但让它返回一个值是没有意义的。您只在该功能中设置适配器。确实有一个onclicklistener并在点击selectedDistrict设置。但这只会在用户点击某个项目时发生。你的功能早就回来了。现在,您的函数返回的是调用它之前的静态selectedDistrict值。