1
を選択していないことは、ここに私のコードスピナーは、最初の項目
Spinner agegroup;
public void generateAgeGroupSpinner() {
final List<AgeGroupModel> ageGroup = new ArrayList<>();
agegroup = (Spinner) findViewById(R.id.ageGroupSpinner);
// Spinner click listener
agegroup
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position)
.toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
// Spinner Drop down elements
ageGroupList.add(new AgeGroupModel(AgeGroup.BELOW18.getId(), AgeGroup.BELOW18.getName()));
ageGroupList.add(new AgeGroupModel(AgeGroup.BETWEEN_19_35.getId(), AgeGroup.BETWEEN_19_35.getName()));
ageGroupList.add(new AgeGroupModel(AgeGroup.BETWEEN_36_45.getId(), AgeGroup.BETWEEN_36_45.getName()));
ageGroupList.add(new AgeGroupModel(AgeGroup.ABOVE45.getId(), AgeGroup.ABOVE45.getName()));
// Creating adapter for spinner
ArrayAdapter<AgeGroupModel> agedataAdapter = new ArrayAdapter<AgeGroupModel>(
this, android.R.layout.simple_spinner_item, ageGroup);
// Drop down layout style - list view with radio button
agedataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
agegroup.setAdapter(typeSpinnerAdapterForAge);
agegroup.setOnItemSelectedListener(typeSelectedListener);
agegroup.setOnTouchListener(typeSpinnerTouchListener);
}
設定スピナーアダプタ typeSpinnerAdapterForAgeだ
private SpinnerAdapter typeSpinnerAdapterForAge = new BaseAdapter() {
private TextView text;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflator.inflate(R.layout.row_spinner, null);
}
text = (TextView) convertView.findViewById(R.id.spinnerTarget);
if (!selected) {
if (parent.equals(agegroup)) {
text.setText(getText(R.string.age_group));
}
} else {
text.setText(ageGroupList.get(position).getName());
}
return convertView;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public AgeGroupModel getItem(int position) {
return ageGroupList.get(position);
}
@Override
public int getCount() {
return ageGroupList.size();
}
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
if (convertView == null) {
convertView = mInflator.inflate(
android.R.layout.simple_spinner_dropdown_item, null);
}
text = (TextView) convertView.findViewById(android.R.id.text1);
text.setText(ageGroupList.get(position).getName());
return convertView;
}
;
};
typeSpinnerSearchListener
スピナードロップダウンの最初の値が最初に選択されていない
private View.OnTouchListener typeSpinnerTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
selected = true;
((BaseAdapter) typeSpinnerAdapter).notifyDataSetChanged();
return false;
}
};
、 他のいくつかを選択する必要がある最初の項目でコードが正常に機能します
デフォルトでは、どの項目が選択されていますか?おそらく最初の項目です。そのため、 'setOnItemSelectedListener'は呼び出されません –