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