用于RSS阅读器的Android ListView中的自定义字体

时间:2013-05-24 17:19:11

标签: android android-listview

我想在Android ListView中显示来自Rss Feed的新闻(孟加拉语)。运行应用程序时屏幕上没有任何显示。我正在分享我的代码。我很高兴,如果你们告诉我,如果我在这里做错了什么

  

MainActivity.java

package com.andodev.android.reader;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.andodev.android.reader.data.RssItem;
import com.andodev.android.reader.listeners.ListListener;
import com.andodev.android.reader.util.RssReader;

public class NewsActivity extends Activity {
    /** 
     * This method creates main application view
     */
    List<RssItem> listItems = new ArrayList<RssItem>();
    CustomAdapter adapt;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set view
        setContentView(R.layout.main);

        try {
            // Create RSS reader
            RssReader rssReader = new RssReader("http://www.amaderbarisal.com/feed/");
            // Get a ListView from main view
            ListView itcItems = (ListView) findViewById(R.id.list);
            adapt = new CustomAdapter(this, listItems);
                itcItems.setAdapter(adapt);
            // Set list view item click listener
            itcItems.setOnItemClickListener(new             ListListener(rssReader.getItems(), this));

        } catch (Exception e) {
            Log.e("ITCRssReader", e.getMessage());
        }

    }

}
  

CustomAdapter

/**
 * 
 */
package com.andodev.android.reader;

import java.util.List;

import com.andodev.android.reader.data.RssItem;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CustomAdapter extends ArrayAdapter<RssItem> {

    List<RssItem> listItems;
    Context context;
    public CustomAdapter(Context context,List<RssItem> listItems) {
        super(context,R.layout.custom);
        // TODO Auto-generated constructor stub
        this.context = context;
        this.listItems = listItems;

    }

    public RssItem getItem(int position){
        return listItems.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v = convertView;
        RssHolder holder = new RssHolder();
        if(convertView == null){
             LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             v = inflater.inflate(R.layout.custom, null);
             TextView tv = (TextView)v.findViewById(R.id.name);
             Typeface myTYpe = Typeface.createFromAsset(context.getAssets(), "solaiman-lipi.ttf");
             tv.setTypeface(myTYpe);
             holder.RssTitle = tv;
             v.setTag(holder);
        }
        else{
             holder = (RssHolder) v.getTag();
             RssItem p = listItems.get(position);
             holder.RssTitle.setText(p.getTitle());
        }
        return v;
    }

    private static class RssHolder{
        public TextView RssTitle;
    }

}
  

custom.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:textStyle="bold"
        />

</LinearLayout>
  

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

如果TextViewconvertView,您不会在null中设置文字。

而不是:

    if(convertView == null){
         LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         v = inflater.inflate(R.layout.custom, null);
         TextView tv = (TextView)v.findViewById(R.id.name);
         Typeface myTYpe = Typeface.createFromAsset(context.getAssets(), "solaiman-lipi.ttf");
         tv.setTypeface(myTYpe);
         holder.RssTitle = tv;
         v.setTag(holder);
    }
    else{
         holder = (RssHolder) v.getTag();
         RssItem p = listItems.get(position);
         holder.RssTitle.setText(p.getTitle());
    }

使用

    ViewHolder holder;
    if (convertView == null) {
        convertView = LayoutInflater.from(context).inflate(R.layout.custom, null);

        holder = new ViewHolder();

        Typeface typeface = Typeface.createFromAsset(context.getAssets(), "solaiman-lipi.ttf")

        holder.rssTitle = (TextView) convertView.findViewById(R.id.name);
        holder.rssTitle.setTypeface(typeface);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.rssTitle.setText(listItems.get(position).getTitle());

    return convertView;