2017-08-25 39 views
1

我有一個適配器,我傳遞數組的顏色和文本數組>它不顯示顏色數組的任何項目?爲什麼適配器無法顯示顏色數組?

public class HoursFromToAdapter extends BaseAdapter{ 
     private Context context; 
     private String[] mHoursRenge; 
     private int[] mColors; 

     private TextView mHoursRangeTxt; 
     private CardView mCardView; 

     public HoursFromToAdapter(Context context, String[] hoursRenge , int[] colors) { 
      this.context=context; 
      this.mHoursRenge=hoursRenge; 
      this.mColors=colors; 
     } 
     @Override 
     public int getCount() { 
      /*return number of elements inside this array*/ 
      return mHoursRenge.length; 
     } 
     @Override 
     public Object getItem(int position) { 
      /*return the item at posion -position-*/ 
      return null; 
     } 

     @Override 
     public long getItemId(int position) { 
      /*return the id of the row which in this case the index of the array*/ 
      return 0; 
     } 


     @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 
     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      inflater.inflate(R.layout.day_item,parent,false); 
      View v; 

      LinearLayout ln; 
      if(convertView == null) { 
       v = new View(context); 
       v = inflater.inflate(R.layout.day_item, null); 
       ln=(LinearLayout)v.findViewById(R.id.card_containner); 

       mHoursRangeTxt= (TextView) v.findViewById(R.id.text_hours_from_to); 
       mHoursRangeTxt.setText(mHoursRenge[position]); 

       mCardView=(CardView)v.findViewById(R.id.card_day_item); 
       mCardView.setCardBackgroundColor(mColors[position]); 


      }else { 
       v = convertView; 
      } 


      return v; 
     } 
    } 

時,我的色彩傳遞數組從其它類豈不顯示,但顯示的文本的陣列以及

String hoursArray[]={"7 am : 8 am" ,"8 am : 9 am","9 am : 10 am","10 am : 11 am","11 am : 12 pm","12 pm : 1 pm","1 pm : 2 pm","2 pm : 3 pm"}; 
     int colorsArray[]={R.color.colorOrange,R.color.colorRed,R.color.colorOrange,R.color.colorOrange,R.color.colorRed,R.color.colorOrange,R.color.colorRed,R.color.colorOrange}; 
     mHoursFromToAdapter=new HoursFromToAdapter(HomeActivity.this,hoursArray,colorsArray); 

什麼是不能顯示對cardView顏色陣列的問題?

回答

1

setCardBackgroundColor預計顏色,而不是resId呈現顏色。

變化

mCardView.setCardBackgroundColor(mColors[position]) 

mCardView.setCardBackgroundColor(ContextCompat.getColor(context, mColors[position])) 

我也動

mHoursRangeTxt= (TextView) v.findViewById(R.id.text_hours_from_to); 
mHoursRangeTxt.setText(mHoursRenge[position]); 

mCardView=(CardView)v.findViewById(R.id.card_day_item); 
mCardView.setCardBackgroundColor(mColors[position]); 

if else條款外,實施持有者模式

+0

感謝它運作良好 –

相關問題