Highlight a preselected item
Let's say I have 10 items in a list of counters. And position number 3 is already selected. Now the thing is, when the user wants to change their selection, I want to give some indication that it is (position number 3) that is already selected. I want to achieve this with Check-mark or set some kind of background or similar.
Can anyone help me with this issue?
+3
source to share
1 answer
I am using a dedicated adapter for this function. Just stretch it with BaseAdapter
and inflate your views for controls Spinner
and Droppdown
.
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;
}
}
0
source to share