android--Adapter用法总结

ArrayAdapter支持泛型操作,最为简单,只能展示一行字。

SimpleAdapter有最好的扩充性,可以自定义出各种效果。

SimpleCursorAdapter以方便地把数据库的内容以列表的形式展示出来。

使用自定义的BaseAdapter实现LIstView的展示

package com.example.mynews.adapter;

import java.util.ArrayList;

import com.example.mynews.R;
import com.example.mynews.entity.News;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class NewsAdapter extends BaseAdapter {

	private ArrayList<News> mlist;
	private Context mContext;

	private static final int VIEW_ITEM_TITLE = 1;
	private static final int VIEW_ITEM_TITLE_SINGLE_IMG = 2;
	private static final int VIEW_ITEM_TITLE_SINGLE_IMGS = 3;
	private static final int ITEM_TYPE_COUNT = 2;

	public NewsAdapter(Context mContext, ArrayList<News> mlist) {
		super();
		this.mlist = mlist;
		this.mContext = mContext;
	}

	@Override
	public int getCount() {
		return this.mlist == null ? 0 : this.mlist.size();
	}

	@Override
	public Object getItem(int position) {
		return this.mlist.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		int imgid = this.mlist.get(position).getImgId();
		if (imgid > 0) {
			return VIEW_ITEM_TITLE_SINGLE_IMG;
		}
		return VIEW_ITEM_TITLE;
	}

	@Override
	public int getViewTypeCount() {
		return super.getViewTypeCount();
	}

	ViewHolder holder = null;
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
	 
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(this.mContext);
			convertView = inflater.inflate(R.layout.listview_item, null);
			holder = new ViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.rq = (TextView) convertView.findViewById(R.id.rq);
			holder.ly = (TextView) convertView.findViewById(R.id.ly);
			holder.pl = (TextView) convertView.findViewById(R.id.pl);
			holder.img = (ImageView) convertView.findViewById(R.id.tp);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final News news = this.mlist.get(position);
		holder.title.setText(news.getTitle());
		holder.rq.setText(news.getRq());
		holder.ly.setText(news.getLy());
		holder.pl.setText(news.getPl());

		if (getItemViewType(position) == VIEW_ITEM_TITLE) {
			holder.img.setVisibility(View.GONE);
		} else if (getItemViewType(position) == VIEW_ITEM_TITLE_SINGLE_IMG)  {
			holder.img.setVisibility(View.VISIBLE);
			holder.img.setImageResource(news.getImgId());
		}else if(getItemViewType(position) == VIEW_ITEM_TITLE_SINGLE_IMGS){
			holder.img.setVisibility(View.VISIBLE);
			holder.img.setImageResource(news.getImgId());
			holder.img.setImageResource(news.getImgId());
			holder.img.setImageResource(news.getImgId());
		}
		return convertView;
	}

	public static class ViewHolder {
		TextView title;
		TextView rq;
		TextView ly;
		TextView pl;
		ImageView img;
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值