I have a spinner that I create like this:
operatorSpinner = new Spinner(this);
operatorSpinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, operatorSpinnerArray);
operatorSpinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
operatorSpinner.setAdapter(operatorSpinnerArrayAdapter);
The layout, spinner_item is this:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="28dip"
android:layout_height="28dip"
android:gravity="center"
android:background="@drawable/circle"
android:textColor="#a4a3a3" />
My reason for applying this layout is to add a circle to the background of each item in the Spinner's dropdown list. However, I only want the circle background applied under certain conditions, so I need a way to programmatically remove either the DropDownViewResource or the layout background within that resource.
I tried this:
operatorSpinnerArrayAdapter.setDropDownViewResource(0);
but it resulted in the app crashing when that code executes. I found a way to achieve what I want by replacing the adapter with a new adapter like this:
operatorSpinnerArrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, operatorSpinnerArray);
operatorSpinner.setAdapter(operatorSpinnerArrayAdapter);
However, my 'feeling' is that creating a new adapter every time I want to change its background is not the best approach. This is my first app I'm trying to develop so I'm not experienced, but I suspect this approach may eat up resources if repeated many times. Can anybody suggest a better approach or confirm/falsify my concerns? Thanks
I recommend you to make a custom adapter by which you can play with whatever the customization you want.
Take a look at http://theopentutorials.com/tutorials/android/listview/android-custom-listview-with-image-and-text-using-baseadapter/
Don't extend your custom adapter to Array adapter but BaseAdapter.Yo need to pass a RowItem like
class RowItem{
...
boolean isConditionSatisfied;
...
}
and in you adapter override the getView() as
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
RowItem rowItem = (RowItem) getItem(position);
holder.txtDesc.setText(rowItem.getDesc());
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
if(isConditionSatisfied){
convertView.setBackground(R.drawable.your_drawable);
}
return convertView;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.