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 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 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;
	}

}


你可能感兴趣的:(android--Adapter用法总结)