2016-10-17 38 views
0

我正在使用AlertDialog顯示一些調試信息。我只是有一個我放入信息的列表。爲了使它更具可讀性,我想根據它們的類型來更改列表中各個項目的背景顏色。不幸的是,我似乎無法得到我想要的顏色。下面是我使用的適配器的視圖:Android在AlertDialog中使用適配器時更改背景顏色

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View v = super.getView(position, convertView, parent); 
    TextView tv = (TextView) v.findViewById(android.R.id.text1); 
    RuleDebugItem item = mData.get(position); 

    tv.setSingleLine(false); 
    if(item.type.equalsIgnoreCase(Field.VARIABLE)) { 
     tv.setText(item.ruleDebugText); 
     tv.setTextSize(18); 
     v.setBackgroundColor(color.bluelight); 
     tv.setTypeface(null, Typeface.BOLD); 
     tv.setTextColor(ContextCompat.getColor(mContext,android.R.color.white)); 
    } 
    else if (item.type.equalsIgnoreCase(Field.FUNCTION)) { 
     tv.setText(item.ruleDebugText); 
     tv.setTextSize(16); 
     v.setBackgroundColor(color.greenlight); 
     tv.setTypeface(null, Typeface.BOLD); 
     tv.setTextColor(ContextCompat.getColor(mContext,android.R.color.white)); 
    } 
    else if (item.type.equals("Condition")) { 
     tv.setText(" " + item.ruleDebugText); 
     tv.setTextSize(14); 
     tv.setTypeface(null, Typeface.NORMAL); 
     tv.setTextColor(ContextCompat.getColor(mContext,android.R.color.black)); 
    } 
    return v; 
} 

我期待v.setBackgroundColor(color.greenlight);改變底色的顏色。我想我可以建立自己的對話框,但是我真的不想在這上面花費很多時間,因爲它可能是一次性代碼。

謝謝!

+0

您可以在onBindViewHolder方法改變這個可以是在容易控制 –

+0

@Selvin作爲說,扔掉代碼和正在進行的工作。條件(和其他項目)將在稍後添加。如果已經使用ArrayAdpater和AlertDialog解決了問題,請給我發送鏈接。謝謝。 –

+0

Rahul - ArrayAdapter似乎沒有onBindViewHolder方法。還是暗示我做了一些不同的事情? –

回答

0

所以答案不是我期待的。結果AlertDialog不接受標準顏色,而是需要從Android樣式中獲得它們。不知道這是否是由於某人在我們身邊設置了樣式(可能因爲我沒有編寫基本代碼),或者這是AlertDialog的性質。所以,如果別人遇到這種情形,這裏是最後的代碼(請注意,這只是一次性調試代碼,它不優化):

public class RuleDebugItemAdapter extends ArrayAdapter<RuleDebugItem> { 
Context mContext; 
int mLayoutResourceId;  
ArrayList<RuleDebugItem> mData; 

public RuleDebugItemAdapter(Context context, int resource, ArrayList<RuleDebugItem> data) { 
    super(context, resource, data); 
    mContext = context; 
    mLayoutResourceId = resource; 
    mData = data; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View v = super.getView(position, convertView, parent); 
    TextView tv = (TextView) v.findViewById(android.R.id.text1); 
    RuleDebugItem item = mData.get(position); 

    tv.setSingleLine(false); 
    if (item.type.equalsIgnoreCase(Field.VARIABLE)) { 
     tv.setText(item.ruleDebugText); 
     tv.setTextSize(18); 
     tv.setTypeface(null, Typeface.BOLD); 
     v.setBackgroundColor(ContextCompat.getColor(mContext,android.R.color.white)); 
     tv.setTextColor(ContextCompat.getColor(mContext,android.R.color.black)); 
    } 
    else if (item.type.equalsIgnoreCase(Field.FORMRULE)) { 
     tv.setText(item.ruleDebugText); 
     tv.setTextSize(18); 
     tv.setTypeface(null, Typeface.BOLD); 
     v.setBackgroundColor(ContextCompat.getColor(mContext,android.R.color.white)); 
     tv.setTextColor(ContextCompat.getColor(mContext,android.R.color.black)); 
    } 
    else if (item.type.equalsIgnoreCase(Field.FUNCTION)) { 

     tv.setTextSize(16); 
     if (item.success) { 
      v.setBackgroundColor(ContextCompat.getColor(mContext,android.R.color.holo_green_light)); 
      tv.setText(Field.SPACE + item.ruleDebugText); 
     } 
     else { 
      v.setBackgroundColor(ContextCompat.getColor(mContext,android.R.color.holo_blue_bright)); 
      tv.setText("Init " + item.ruleDebugText); 
     } 

     tv.setTypeface(null, Typeface.BOLD); 
     tv.setTextColor(ContextCompat.getColor(mContext,android.R.color.white)); 
    } 
    else if (item.type.equalsIgnoreCase(Field.ACTION)) { 

     tv.setTextSize(16); 
     if (item.success) { 
      v.setBackgroundColor(ContextCompat.getColor(mContext,android.R.color.holo_green_light)); 
      tv.setText(Field.SPACE + item.ruleDebugText); 
     } 
     else { 
      v.setBackgroundColor(ContextCompat.getColor(mContext,android.R.color.holo_blue_bright)); 
      tv.setText("Pre-" + item.ruleDebugText); 
     } 

     tv.setTypeface(null, Typeface.BOLD); 
     tv.setTextColor(ContextCompat.getColor(mContext,android.R.color.white)); 
    } 
    else if (item.type.equals(Field.CONDITION)) { 
     tv.setText(item.ruleDebugText); 
     tv.setTextSize(16); 
     tv.setTypeface(null, Typeface.NORMAL); 
     if (item.success) 
      v.setBackgroundColor(ContextCompat.getColor(mContext,android.R.color.holo_green_light)); 
     else 
      v.setBackgroundColor(ContextCompat.getColor(mContext,android.R.color.holo_red_light)); 
     tv.setTextColor(ContextCompat.getColor(mContext,android.R.color.white)); 
    } 
    return v; 
} 
}