android - arrayadapter 更改字体时出错

标签 android android-arrayadapter

我正在尝试修改 MyKong 中的代码,添加一些更改 TextView 字体的行,但是当我尝试运行时遇到强制关闭。

这里是 MobileArrayAdapter.java

> package com.mkyong.android.adaptor;
> 
> import com.mkyong.android.R;
> 
> import android.content.Context; import
> android.content.res.AssetManager; import android.graphics.Color;
> import android.graphics.Typeface; import android.view.LayoutInflater;
> import android.view.View; import android.view.ViewGroup; import
> android.widget.ArrayAdapter; import android.widget.ImageView; import
> android.widget.TextView;
> 
> public class MobileArrayAdapter extends ArrayAdapter<String> {
>   private final Context context;  private final String[] values;
> 
>   public MobileArrayAdapter(Context context, String[] values) {
>       super(context, R.layout.list_mobile, values);       this.context =
> context;      this.values = values;   }
> 
>   @Override   public View getView(int position, View convertView,
> ViewGroup parent) {       LayoutInflater inflater = (LayoutInflater)
> context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        View
> rowView = inflater.inflate(R.layout.list_mobile, parent, false);
>       TextView textView = (TextView) rowView.findViewById(R.id.label);
>       ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
> // Here starts my modify
>               Context context1 = null;
>               AssetManager assetManager = context1.getResources().getAssets();        Typeface typeface = Typeface.createFromAsset(assetManager,
> "fonts/principale.ttf");      textView.setTextColor(Color.RED);
>       textView.setTypeface(typeface); // Here ends
>               textView.setText(values[position]);
> 
>       // Change icon based on name        String s = values[position];
> 
>       System.out.println(s);
> 
>       if (s.equals("WindowsMobile")) {
>           imageView.setImageResource(R.drawable.windowsmobile_logo);      } else
> if (s.equals("iOS")) {
>           imageView.setImageResource(R.drawable.ios_logo);        } else if
> (s.equals("Blackberry")) {
>           imageView.setImageResource(R.drawable.blackberry_logo);         } else {
>           imageView.setImageResource(R.drawable.android_logo);        }
> 
>       return rowView;     } }

这里列出MobileActivity.java

package com.mkyong.android;

import com.mkyong.android.adaptor.MobileArrayAdapter;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.view.View;

public class ListMobileActivity extends ListActivity {

    static final String[] MOBILE_OS = new String[] { "Android", "iOS",
            "WindowsMobile", "Blackberry"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setListAdapter(new MobileArrayAdapter(this, MOBILE_OS));        
    }
}

这里是 list_mobile.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/logo"
        android:layout_width="50px"
        android:layout_height="50px"
        android:layout_marginLeft="5px"
        android:layout_marginRight="20px"
        android:layout_marginTop="5px"
        android:src="@drawable/windowsmobile_logo" >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="30px" >
    </TextView>

</LinearLayout>

现在,我知道我问了很多,但我该如何解决呢?从自定义适配器更改字体的正确方法是什么? 感谢您的回复。

最佳答案

MobileArrayAdapter 类 getView 方法中,您使用 null 上下文来访问 AssetManager,因此更改

Context context1 = null; 

Context context1 = context;

关于android - arrayadapter 更改字体时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14032590/

相关文章:

android - 从 Android Studio 运行应用程序不再保存到我的 Android 设备上

android-arrayadapter - 如何使用 Leanback 库在 Android TV 中高效实现大型单个列表

java - android: fragment 未加载 fragment AsyncTask ArrayAdapter

java - 将动态 ArrayList 添加到 ArrayAdapter 并显示在屏幕上

android - 查找哪个依赖项包括什么

android - 在android中从右上角、左下角、右下角旋转动画android?

java - 为什么我们提出接口(interface)改造请求

ListView中的Android自定义ArrayAdapter,ListView onClick适用于模拟器但不适用于手机

fragment 中的 Android ListView

android字节数组正在缩短数字?