我正在尝试根据在微调器区域,区域和vdc上选择的内容在微调器present_zone,present_district和present_vdc上设置条目。
通过方法setSpinnerenries()设置条目,该方法根据传递的文本从string.xml设置条目。
但是,当选中CheckBox时,setSelection仅将条目设置为第一个微调器(present_zone)。然后,我必须再次打勾才能将条目设置为第二个微调器。 我不知道为什么只需单击一次即可设置所有微调器条目。 //根据所选区域在strings.xml中设置微调器中的当前区域条目,否则设置为空
present_zone.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
if(arg2 > 0){
setSpinnerEntries(present_zone, present_district);
}
else{
present_zone.setSelection(0);
setSpinnerNull(present_district);
setSpinnerNull(present_vdc);
}
} public void onNothingSelected(AdapterView<?> arg0) {}
});
//Sets present VDC entries in spinner from strings.xml if district is chosen otherwise sets null
present_district.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
if(arg2 > 0){
Toast.makeText(Form.this,arg2+"",Toast.LENGTH_SHORT).show();
setSpinnerEntries(present_district, present_vdc);
}
else{
setSpinnerNull(present_vdc);
}
}
public void onNothingSelected(AdapterView<?> arg0) {}
});
//sets entries in spinners present_zone, present_district and present_vdc when ticked ,the spinners are set based on what isSelectedItem in zone, district and vdc
tick_address_copy_paste.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
int copy_zone = (int) zone.getSelectedItemId();
int copy_district = (int) district.getSelectedItemId();
int copy_vdc = (int) vdc.getSelectedItemId();
if (isChecked) {
if(copy_zone>0){
present_zone.setSelection(copy_zone, false);
setSpinnerEntries(present_district, present_vdc);
if(copy_district>0){
present_district.setSelection(copy_district, false);
present_vdc.setSelection(copy_vdc, false);
}
}
}
}
});
//sets entries to spinner from strings.xml file
private void setSpinnerEntries(Spinner setFrom,Spinner setTo) {
String text = setFrom.getSelectedItem().toString();
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(Form.this,
getStringIdentifier(Form.this, text), android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
setTo.setAdapter(adapter);
}
//returns resource identifier from text
private static int getStringIdentifier(Context context, String name) {
return context.getResources().getIdentifier(name, "array", context.getPackageName());
}
这是我的布局xml:
<LinearLayout
android:id="@+id/zone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Zone"
android:textColor="@color/colorAccent" />
<Spinner
android:id="@+id/present_zone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/zones"
android:spinnerMode="dialog" />
</LinearLayout>
<LinearLayout
android:id="@+id/district"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" District"
android:textColor="@color/colorAccent" />
<Spinner
android:id="@+id/present_district"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog" />
</LinearLayout>
<LinearLayout
android:id="@+id/village"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Village"
android:textColor="@color/colorAccent" />
<Spinner
android:id="@+id/present_vdc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog" />
</LinearLayout>