我有Spinner
我在其中填充国家/地区。我将澳大利亚设置在微调器的第0位。当我第一次选择它时,它显示为已选中,但实际上它未被选中。但是当我从列表中选择任何其他项目然后选择澳大利亚时,它将被选中。
任何人都可以帮我解释为什么第一次没有选择0索引?
代码
final MySpinnerAdapter adapter_country = new MySpinnerAdapter(SignUp.this, android.R.layout.simple_spinner_item, countryname);
adapter_country.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_country.setPrompt("Select Country");
spinner_country.setAdapter(adapter_country);
Collections.sort(countryname);
countryname.add(0, "Australia");
countryname.add(0, "Country");
countryname.remove(15);
spinner_country.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_UP:
if (spinner_check == 1) {
if (spinner_remove_check == 1) {
} else {
adapter_country.notifyDataSetChanged();
countryname.remove(0);
}
adapter_country.notifyDataSetChanged();
spinner_country.performClick();
spinner_remove_check=1;
}
break;
}
return true;
}
});
spinner_country.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
country_pos = position;
String data = countryname.get(position) ;
if (data.equals("Country")) {
country_check = false;
// countryname.remove(0);
spinner_check = 1;
((TextView) parent.getChildAt(0)).setTextColor(Color.rgb(148, 150, 148));
Get_gender();
} else {
HashMap<String, String> selectedData = new HashMap<String, String>();
selectedData = countrylist.get(position);
String selectedcountry_id =selectedData.get(TAG_ID);
countryid=Integer.parseInt(selectedcountry_id);
country_check=true;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
countryname
是ArrayList
。我也在做adapter_country.notifyDataSetChanged();
但遇到同样的问题。