Reputation: 2183
Say I have 10 items in the spinner list. And item number 3 is already selected. Now the thing is when user wants to change his selection, I want to give some kind of indication that this(item number 3) which is already selected item. I want to achieve this via a Check-mark or setting some kind of background or in similar ways.
Can anybody please help me with this issue?
Upvotes: 4
Views: 1808
Reputation: 3881
I use custom adapter for this feature. Just extend it from BaseAdapter
and inflate your views for Spinner
and Droppdown
control.
List<String> stagesValues = new ArrayList<>(stagesResults.values());
mStageSpn.setAdapter(new DropdownAdapter(mContext, stagesValues, mStageSpn));
public class DropdownAdapter extends BaseAdapter {
private final LayoutInflater mInflater;
private List<String> mData;
private Spinner mStageSpn;
public DropdownAdapter(Context context, List<String> data, Spinner stageSpn) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mData = data;
mStageSpn = stageSpn;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = mInflater.inflate(android.R.layout.simple_spinner_item, null);
((TextView) view.findViewById(android.R.id.text1)).setText(mData.get(mStageSpn.getSelectedItemPosition()));
return view;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view = mInflater.inflate(R.layout.spinner_item, null);
if (mStageSpn.getSelectedItemPosition() == position)
view.setBackgroundColor(Color.RED);
((TextView) view.findViewById(R.id.text_id)).setText(mData.get(position));
return view;
}
}
Upvotes: 1