微调器setSelected无法正常工作

时间:2017-11-22 07:17:34

标签: java android spinner

我有一个个人资料活动,用户首次在那里选择国家/地区,之后当用户希望更改国家/地区时他将进入个人资料活动,他应该在旋转器中看到之前选择的国家/地区然后他必须能够从微调器中选择另一个国家。 我正在尝试更新我的个人资料活动,我有一个名为country的微调器。我正在使用set来获取之前选择的值。我正在获取值但是当我试图改变该值时它没有发生。 Country_value是字符串 下面是微调器的代码:

country_adapter_list = new ArrayAdapter(MyProfile.this, R.layout.support_simple_spinner_dropdown_item,Country_listItems);
country = (Spinner) findViewById(R.id.sp_country);
country.setAdapter(country_adapter_list);

country.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
country.setSelection(Country_listItems.indexOf(country_value));
country_data = Country_listItems.get(position);
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
});

3 个答案:

答案 0 :(得分:0)

OnItemSelected()方法将选择position列表中的项目。 setSelection()将在内部调用onItemSelected()以在微调器中设置所选项。因此,在setSelection()内调用onItemselected()对您无济于事。

答案 1 :(得分:0)

您的代码工作正常,但如果在您设置的arraylist中找不到“country_value”,则不会在Spinner上设置选择。

Video Link

答案 2 :(得分:0)

尝试以下代码。

If arraySize > 3 Then

}