重写ArrayAdapter

package org.yang.android.adapter.util;



import org.yang.android.adapter.demo.R;
import org.yang.android.adapter.model.Book;

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

public class IAdapter extends ArrayAdapter<Object> {
	
	private int resources;
	private LayoutInflater inflater;
	
	public IAdapter(Context context, int resources, Book[] books)
	{
		super(context, resources, books);
		this.resources = resources;
		this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = inflater.inflate(resources, null);
        
        Book bi = (Book) this.getItem(position);
        if (bi == null) {
            return null;
        }
        
        TextView nameView = (TextView)convertView.findViewById(R.id.bname);
        nameView.setText(bi.getName());
        
        ImageView bicon = (ImageView)convertView.findViewById(R.id.bimage);
        bicon.setBackgroundDrawable(bi.getIcon());
        
        TextView bdescView = (TextView)convertView.findViewById(R.id.bdesc);
        bdescView.setText(bi.getDesc());
        
        TextView bpress = (TextView)convertView.findViewById(R.id.bpress);
        bpress.setText(bi.getPress());
        
        TextView bprices = (TextView)convertView.findViewById(R.id.bprices);
        bprices.setText(bi.getPrices());
        
		return convertView;
	}
}
package org.yang.android.adapter.demo;

import org.yang.android.adapter.model.Book;
import org.yang.android.adapter.util.IAdapter;

import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

public class AdapterDemoActivity extends Activity {
    /** Called when the activity is first created. */
	ListView lv;
	Book[] books = new Book[5];
	
	AssetManager assets;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        lv = (ListView)this.findViewById(R.id.lv);
        
        assets = this.getAssets();
        try {
			books[0] = new Book("linux", Drawable.createFromStream(assets.open("linux.jpg"), null), "desc", "人民", "36.6");
			
			books[1] = new Book("linux", Drawable.createFromStream(assets.open("linux.jpg"), null), "desc", "人民", "36.6");
			
			books[2] = new Book("linux", Drawable.createFromStream(assets.open("linux.jpg"), null), "desc", "人民", "36.6");
			
			books[3] = new Book("linux", Drawable.createFromStream(assets.open("linux.jpg"), null), "desc", "人民", "36.6");
			
			books[4] = new Book("linux", Drawable.createFromStream(assets.open("linux.jpg"), null), "desc", "人民", "36.6");
		} catch (Exception e) {
			// TODO: handle exception
			Log.d("app 读到文件出错!", e.toString());
		}
		IAdapter adapter = new IAdapter(this, R.layout.book, books);
		lv.setAdapter(adapter);
    }
}

转载于:https://my.oschina.net/linuxyangzhiyong/blog/54745

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值