package com.example.mylistview3.adapter;
import java.util.ArrayList;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.chocolate.model.Chocolate;
import com.example.mylistview3.R;
public class ChocolateAdapter extends BaseAdapter {
// 数据源
private ArrayList<Chocolate> mChocolate;
private LayoutInflater minflater;
Context mContext;
Resources mResources;
public ChocolateAdapter(Context context, ArrayList<Chocolate> list) {
mContext = context;
mChocolate = list;
minflater = LayoutInflater.from(context);
mResources = context.getResources();
}
@Override
public int getCount() {
return mChocolate.size();
}
@Override
public Object getItem(int position) {
return mChocolate.get(position);
}
@Override
public long getItemId(int position) {
return mChocolate.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder vh = new ViewHolder();
if (view == null) {
view = minflater.inflate(R.layout.view_chocolate_item, null);
vh.tvId = (TextView) view.findViewById(R.id.tv_serial);
vh.ivPic = (ImageView) view.findViewById(R.id.iv_image);
vh.tvName = (TextView) view.findViewById(R.id.tv_name);
vh.tvDescription = (TextView) view
.findViewById(R.id.tv_description);
view.setTag(vh);
} else {
vh = (ViewHolder) view.getTag();
}
Chocolate c = mChocolate.get(position);
vh.tvId.setText(String.valueOf(c.getId()));
// vh.ivPic.setImageResource(c.getImgReaId());
Bitmap bm = BitmapFactory.decodeResource(mResources, c.getImgReaId());
vh.ivPic.setImageBitmap(bm);
vh.tvName.setText(c.getName());
vh.tvDescription.setText(c.getDescription());
// View view = getLayoutInflater().inflate(
// R.layout.view_chocolate_item, null);
// tvId = (TextView) view.findViewById(R.id.tv_serial);
// ivPic = (ImageView) view.findViewById(R.id.iv_image);
// tvName = (TextView) view.findViewById(R.id.tv_name);
// tvDescription = (TextView)
// view.findViewById(R.id.tv_description);
//
// Chocolate c = mChocolate.get(position);
// tvId.setText(String.valueOf(c.getId()));
// ivPic.setImageResource(c.getImgReaId());
// tvName.setText(c.getName());
// tvDescription.setText(c.getDescription());
return view;
}
private class ViewHolder {
TextView tvId;
ImageView ivPic;
TextView tvName;
TextView tvDescription;
}
}
import java.util.ArrayList;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.chocolate.model.Chocolate;
import com.example.mylistview3.R;
public class ChocolateAdapter extends BaseAdapter {
// 数据源
private ArrayList<Chocolate> mChocolate;
private LayoutInflater minflater;
Context mContext;
Resources mResources;
public ChocolateAdapter(Context context, ArrayList<Chocolate> list) {
mContext = context;
mChocolate = list;
minflater = LayoutInflater.from(context);
mResources = context.getResources();
}
@Override
public int getCount() {
return mChocolate.size();
}
@Override
public Object getItem(int position) {
return mChocolate.get(position);
}
@Override
public long getItemId(int position) {
return mChocolate.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder vh = new ViewHolder();
if (view == null) {
view = minflater.inflate(R.layout.view_chocolate_item, null);
vh.tvId = (TextView) view.findViewById(R.id.tv_serial);
vh.ivPic = (ImageView) view.findViewById(R.id.iv_image);
vh.tvName = (TextView) view.findViewById(R.id.tv_name);
vh.tvDescription = (TextView) view
.findViewById(R.id.tv_description);
view.setTag(vh);
} else {
vh = (ViewHolder) view.getTag();
}
Chocolate c = mChocolate.get(position);
vh.tvId.setText(String.valueOf(c.getId()));
// vh.ivPic.setImageResource(c.getImgReaId());
Bitmap bm = BitmapFactory.decodeResource(mResources, c.getImgReaId());
vh.ivPic.setImageBitmap(bm);
vh.tvName.setText(c.getName());
vh.tvDescription.setText(c.getDescription());
// View view = getLayoutInflater().inflate(
// R.layout.view_chocolate_item, null);
// tvId = (TextView) view.findViewById(R.id.tv_serial);
// ivPic = (ImageView) view.findViewById(R.id.iv_image);
// tvName = (TextView) view.findViewById(R.id.tv_name);
// tvDescription = (TextView)
// view.findViewById(R.id.tv_description);
//
// Chocolate c = mChocolate.get(position);
// tvId.setText(String.valueOf(c.getId()));
// ivPic.setImageResource(c.getImgReaId());
// tvName.setText(c.getName());
// tvDescription.setText(c.getDescription());
return view;
}
private class ViewHolder {
TextView tvId;
ImageView ivPic;
TextView tvName;
TextView tvDescription;
}
}