Android Adapter推荐写法

package jason.fragmentdemo.adapter;

import nqy.fragmentdemo.R;
import nqy.fragmentdemo.model.ArticleModel;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ArticleTitleFragmentAdapter extends BaseAdapter {

    private LayoutInflater mInflater;

    private ArticleModel[] articles;

    public ArticleTitleFragmentAdapter(Context context, ArticleModel[] articles) {
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.articles = articles;
    }

    @Override
    public int getCount() {
        if (articles != null) {
            return articles.length;
        }

        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (articles != null && position >= 0 && position < articles.length) {
            return articles[position];
        }

        return null;
    }

    @Override
    public long getItemId(int position) {
        if (articles != null && position >= 0 && position < articles.length) {
            return articles[position].id;
        }

        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder holder;

        if (view == null) {
            view = mInflater.inflate(R.layout.item_article_list, parent, false);

            holder = new ViewHolder();
            holder.tv = (TextView)view.findViewById(R.id.list_lable);

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

        holder.tv.setText(articles[position].title);

        return view;
    }

    private class ViewHolder {
        public TextView tv;
    }

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-07-24 16:52  jasonkent27  阅读(150)  评论(0编辑  收藏  举报