简体   繁体   中英

android spinner change the background color for specific items

i have a spinner , i want to change the background color for specific items. i mean if the object is named: country and if it have value section=true so the item of spinner will have background color = blue .

@Override
   public View getView(int position, View convertView, ViewGroup parent) {
 final kamcoReportType report = values.get(position);
  if (convertView == null) {
    Context mContext = this.getContext();
   LayoutInflater vi = (LayoutInflater)   mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    TextView tv=(TextView) convertView.findViewById(R.id.TextView01);
    if (report.isSection())
       tv.setBackgroundColor=blue  //what ever

how can i do that.

On your getView() method you can do:

        if (yourCondition) {
            tv.setBackgroundColor(getContext().getResources().getColor(R.color.blue));
        }

This should do the trick.

@UPDATE

If I got you right, you want to get the current item on your getView() method, right?

I assume your adapter is extending ArrayAdapter<T> . If this is the case, you can get your items using ArrayAdapter getItem(position) method and test it like:

if ((getItem(position)).isSection()) { }

Inside your getView() .

Hope this helps.

Try this:

if (report.isSection())
   tv.setBackgroundColor(Color.parseColor("#0D47A1"));

Reading your question, I understood that you want to access the item of the list. Well here is how you can do that:

   @Override
   public View getView(int position, View convertView, ViewGroup   parent) {
     final kamcoReportType report = values.get(position);
     if (convertView == null) {
         Context mContext = this.getContext();
         LayoutInflater vi = (LayoutInflater)   mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         TextView tv=(TextView) convertView.findViewById(R.id.TextView01);
         if (report.isSection())
         tv.setBackgroundColor(getContext().getResources().getColor(R.color.blue));
     //here is how will you access your list: let say, your data list name is "itemList"

        item=itemList.get(position);//it will return the item from that list



}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM