在简单适配器上使用自定义字体获取错误,错误是什么?

时间:2012-12-30 00:22:17

标签: android layout simpleadapter

我是android的新手......我想在我的android项目中使用自定义字体,所以我把我的代码base on this tutorials。我想在R.id.Headline中使用我的自定义字体和一些xml元素

我的问题是:

- 我的代码中有什么问题吗?

- 是否有任何教程可以从xml构建自定义UI?

这是我的代码

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.concurrent.ExecutionException;

    import android.app.ListActivity;
    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;

    public class MainActivity extends ListActivity {
        private static String url = "http:some url";
        private static final String TAG_HEADLINE = "headline";
        private static final String TAG_PROVIDER = "provider";
        private static final String TAG_TYPE = "deal_type";
        private static final String TAG_BLURB = "blurb";
        private static final String TAG_FEATURED = "featured";
        private static final String TAG_URL = "url";
        private static final String TAG_PRICE = "price";
        private static final String TAG_LOCATION = "location";
        private static final String TAG_IMAGE = "image";
        private static final String TAG_OFFER = "offer";
        private static final String TAG_THUMBNAIL = "thumbnail";
        private static final String TAG_FORMATED = "formatted";
        ArrayList<HashMap<String, String>> dealArrayList = new ArrayList<HashMap<String, String>>();

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.activity_main, menu);
            return super.onCreateOptionsMenu(menu);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            try {
                ArrayList<HashMap<String, String>> deals = new NetraAsycn()
                        .execute(url).get();
                ListAdapter adapter = new SimpleAdapter(this, deals,
                        R.layout.list_item, new String[] { TAG_THUMBNAIL,
                                TAG_FORMATED, TAG_PROVIDER, TAG_LOCATION,
                                TAG_HEADLINE }, new int[] { R.id.thumbnail,
                                R.id.price, R.id.provider, R.id.location,
                                R.id.headline });
  Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/myFont.ttf");
    TextView myTextView = (TextView)findViewById(R.id.headline);
    myTextView.setTypeface(myTypeface);

                setListAdapter(adapter);
                ListView lv = getListView();
                lv.setDividerHeight(0);

            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {

                e.printStackTrace();
            }

        }


        public View getView(int position, View convertView, ViewGroup parent) {
            View rowView = convertView;
             if (rowView == null) {
                  LayoutInflater inflater = context.getLayoutInflater();
                  rowView = inflater.inflate(R.layout.list_item, null);
                  ViewHolder viewHolder = new ViewHolder();
                  viewHolder.text = (TextView) rowView.findViewById(R.id.TextView01);
                  viewHolder.image = (ImageView) rowView
                      .findViewById(R.id.ImageView01);
                  rowView.setTag(viewHolder);
                }
            return rowView;
        }
    }

2 个答案:

答案 0 :(得分:0)

几个星期前我遇到了同样的问题。我认为在这个“createFromAsset”方法中使用目录是不可能的。将您的字体文件复制到父资产目录中。

Typeface myTypeface = Typeface.createFromAsset(getAssets(), "myFont.ttf");

你的logcat说什么?

答案 1 :(得分:0)

您的代码没问题。 我也以同样的方式使用自定义字体。为:

Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/LCALLIG.TTF");          
textView.setTypeface(typeFace); 

只需检查字体名称是否正确,是否正确复制到资产文件夹中 如果是,则显示您的错误或logcat。