2014-03-25 13 views
0

我已經爲我的程序編寫了幾個spinners和列表視圖的自定義適配器。下面是代碼:控制如何寫文本到Android上的spinners和列表

public static class CustomAdapter extends ArrayAdapter<String> { 

    private int fontsize; 
    private int color; 
    private Typeface typeface; 
    private int bgcolor = Color.rgb(50, 50, 50); 
    private int selectedColor = Color.rgb(50, 50, 50); 
    private int selected = -1; 

    public CustomAdapter(Context context, int resource, Typeface tf, int colour, int fsize) { 
     super(context, resource); 
     fontsize = fsize; 
     color = colour; 
     typeface = tf; 
    } 

    public void setBackgroundColor(int bg){ 
     bgcolor = bg; 
    } 

    public void setSelectedColor(int color){selectedColor = color;} 

    public void setSelectedPos(int p){ 
     selected = p; 
     notifyDataSetChanged(); 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     TextView v = (TextView)super.getView(position, convertView, parent); 
     v.setTypeface(typeface); 
     v.setTextSize(fontsize); 
     v.setTextColor(color); 
     if (position == selected) v.setBackgroundColor(selectedColor); 
     else v.setBackgroundColor(bgcolor); 
     return v; 
    } 


    public View getDropDownView(int position, View convertView, ViewGroup parent) { 
     TextView v = (TextView)super.getDropDownView(position, convertView, parent); 
     v.setTypeface(typeface); 
     v.setTextSize(fontsize); 
     v.setTextColor(color); 
     v.setBackgroundColor(bgcolor); 
     return v; 
    } 

} 

現在我想要做的就是控制如何文本在我返回文本視圖實際設置。更具體地說,我想使用SpannableString生成器,就像我在我的應用程序的其他普通文本視圖中一樣。像這樣:

SpannableStringBuilder textstring = new SpannableStringBuilder(); 
    Aux.addText(textstring,cardInfo,Colors.White,Dim.TEXTSIZE_TEXTVIEW,Fonts.Normal); 
    text.setText(textstring, TextView.BufferType.SPANNABLE); 

我想寫這樣的適配器的每個項目。這可以做到嗎?我會appreaciate如果有人可以告訴我如何。

回答

0

以爲我會回答我自己的問題,因爲我找到了解決方案。關鍵是在構造函數中傳遞字符串列表,然後使用相應的文本添加可跨越字符串構建器來返回視圖和下拉視圖。這裏是我的自定義適配器:

public static class CustomAdapter extends ArrayAdapter<String> { 

    private int fontsize; 
    private int color; 
    private Typeface typeface; 
    private int bgcolor = Color.rgb(50, 50, 50); 
    private int selectedColor = Color.rgb(50, 50, 50); 
    private int selected = -1; 
    private boolean setColor = false; 
    private List<String> data; 

    public CustomAdapter(Context context, int resource, Typeface tf, int colour, int fsize, List<String> labels) { 
     super(context, resource,labels); 
     fontsize = fsize; 
     color = colour; 
     typeface = tf; 
     data = labels; 
    } 

    public void setBackgroundColor(int bg){ 
     bgcolor = bg; 
     setColor = true; 
    } 

    public void setSelectedColor(int color){ 
     selectedColor = color; 
     setColor = true; 
    } 

    public void setSelectedPos(int p){ 
     selected = p; 
     notifyDataSetChanged(); 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     TextView v = (TextView)super.getView(position, convertView, parent); 
     SpannableStringBuilder texter = new SpannableStringBuilder(); 
     Aux.addText(texter,data.get(position),color,fontsize,typeface); 
     if (v != null) { 
      v.setText(texter, TextView.BufferType.SPANNABLE); 
      if (setColor) { 
       if (position == selected) v.setBackgroundColor(selectedColor); 
       else v.setBackgroundColor(bgcolor); 
      } 
     } 
     return v; 
    } 


    public View getDropDownView(int position, View convertView, ViewGroup parent) { 
     TextView v = (TextView)super.getDropDownView(position, convertView, parent); 
     SpannableStringBuilder texter = new SpannableStringBuilder(); 
     Aux.addText(texter, data.get(position), color, fontsize, typeface); 
     if (v != null) { 
      v.setText(texter, TextView.BufferType.SPANNABLE); 
      if (setColor) { 
       if (position == selected) v.setBackgroundColor(selectedColor); 
       else v.setBackgroundColor(bgcolor); 
      } 
     } 
     return v; 
    } 

}