如何解决如何在视图模型中填充微调器?
我的微调器应填充字符串。但是不能使用binding.mySpinner
来设置适配器。
目标是拥有两个微调器,第一个是两个国家,分别是美国和加拿大,因此当用户单击美国时,第二个微调器应列出所有州。与加拿大相同。我设法找出要单击的国家,并将该值存储在String中。我想使用简单的If / else语句,但不知道如何填充第二个微调器。
我的xml:`
<Spinner
android:id="@+id/states_spinner"
style="@style/Widget.AppCompat.DropDownItem.Spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
app:layout_constraintBottom_toTopOf="@+id/submit_BTN"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/states_TV" />
`
和我的ViewModel:
public AdapterView.OnItemSelectedListener countryListener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,View view,int position,long id) {
Object item = parent.getItemAtPosition(position).toString();
switch (position){
case 0:
billingInfo.setCountry("Usa");
selectedCountry = billingInfo.getCountry();
List<String> listUsa = new ArrayList<String>(Arrays.asList(SpinnerEntriesUtils.usaArray));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
SuccessPlanApplication.getContext(),android.R.layout.simple_spinner_item,listUsa );
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
break;
case 1:
billingInfo.setCountry("Canada");
selectedCountry = billingInfo.getCountry();
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。