2013-07-19 19 views
1

我必須更改自定義列表適配器上的字體。 我有一個問題,因爲我無法獲得當前的上下文。在customListAdapter上編輯typface

getassets() not exist for the class 
getApplicationcontext() not exist for the class 
getBaseContext() not exist for the class 

我想要得到的觀點的背景下,是沒有錯誤的,但字體不改變

public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     Context context = convertView.getContext(); 
//or 
     Context context2 = parent.getContext(); 

後的代碼我寫

bold = Typeface.createFromAsset(context.getAssets(), "fonts/Jennifer-Lynne.ttf"); 
     italic = Typeface.createFromAsset(context.getAssets(), "fonts/helvetica-italic.ttf"); 
     holder.titoloView.setTypeface(bold); 
     holder.autoreView.setTypeface(italic); 

想法?

+0

對不起(我是一個白癡) 我正在做一個備份副本..我失去了我生命中的3小時-.- 我把工作代碼放在回答中 – Lele

回答

0

這是正確的代碼(字體資產文件夾)

全局變量

Typeface tf, bold, italic; 
    Context prova; 

其中i設置上下文

public CopyOfCustomListAdapterLele(Context context, ArrayList listData) { 
     this.listData = listData; 
     layoutInflater = LayoutInflater.from(context); 
     prova = context; 
    } 

和字體的設定梅託德

public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 


     bold = Typeface.createFromAsset(prova.getAssets(), "fonts/Jennifer-Lynne.ttf"); 



     if (convertView == null) { 
      convertView = layoutInflater.inflate(R.layout.singolopunto, null); 
      holder = new ViewHolder(); 
      holder.titoloView = (TextView) convertView.findViewById(R.id.textView1); //titolo canzone 

      holder.imageView = (SmartImageView) convertView.findViewById(R.id.imageView2); //immagine 
      holder.free = (ImageView) convertView.findViewById(R.id.imageView3); //immagine free 

      holder.titoloView.setTypeface(bold); 


... 
0

將一個上下文傳遞給您的CustomListAdapter類構造函數,並使用它從資產中獲取字體。初始化ViewHolder視圖時設置字體(當getView中的convertView爲null時)。

+0

我無法傳遞上下文......你能寫嗎一排代碼? – Lele