自定义适配器(对象/图像)类

作者在 2015-12-28 23:12:00 发布以下内容
package com.example.android_12_28;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class OneImageAdapter extends BaseAdapter {

	private List<Integer> list;
	private Context context;
	
	public OneImageAdapter(List<Integer> list, Context context) {
		super();
		this.list = list;
		this.context = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int postion) {
		// TODO Auto-generated method stub
		return list.get(postion);
	}

	@Override
	public long getItemId(int postion) {
		// TODO Auto-generated method stub
		return postion;
	}

	@Override
	public View getView(int postion, View view, ViewGroup listView) {
		
		ImageView img = new ImageView(context);
		img.setImageResource(list.get(postion));
		
		return img;
	}

}

public class ObjectAdapter extends BaseAdapter { private Context context; private List<User> list; public ObjectAdapter(Context context, List<User> list) { super(); this.context = context; this.list = list; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return list.get(arg0); } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public View getView(int postion, View view, ViewGroup listView) { //创建加载xml文件的类 LayoutInflater inflater =LayoutInflater.from(context); //加载xml 文件 view=inflater.inflate(R.layout.listview, null); //为xml 文件的控件赋值 TextView name = (TextView) view.findViewById(R.id.textView1); TextView age = (TextView) view.findViewById(R.id.textView2); name.setText(list.get(postion).getName()); age.setText(""+list.get(postion).getAge()); return view; } }

其他图片,xml,对象类略。
android | 阅读 1711 次
文章评论,共0条
游客请输入验证码
浏览239589次
最新评论