自定义BaseAdapter Android ListView GrideView spinner 等 每行包孕图片,TextView,checkbox的显示
/**
* MyAdapter
* TODO
* @knownBugs
*/
public class MyAdapter extends BaseAdapter {
/** the context*/
private Context mContext;
/** the list of the ShopInfoSerialzable*/
private List<ShopInfoSerialzable> mShopData;
/** layoutInflater*/
private LayoutInflater layoutInflater;
/**
* ViewHolder
* TODO
* @knownBugs
*/
class ViewHolder {//这个类变量对应显示的一行的所有组件
/** the picture of the goods*/
ImageView shopIcon;
/** the name of the goods*/
TextView shopName;
/** the checkBox*/
CheckBox checkBox;
}
//构造函数
public MyAdapter(Context aContext,
List<ShopInfoSerialzable> aShopData) {
this.mContext = aContext;
this.mShopData = aShopData;
layoutInflater =
(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mShopData.size();
}
@Override
public Object getItem(int aPosition) {
return mShopData.get(aPosition);
}
@Override
public long getItemId(int aPosition) {
return aPosition;
}
//主要需要重写的方法,得到用来显示的每一行组件
@Override
public View getView(final int aPosition,
View aConvertView, ViewGroup aParent) {
ViewHolder holder = null;
if (aConvertView == null) {
aConvertView =
layoutInflater.inflate(R.layout.shopping_list_row, null);
holder = new ViewHolder();
holder.shopIcon =
(ImageView) aConvertView.findViewById(R.id.shoppingIcon);
aConvertView.setTag(holder);//这里就设定了图片,可以从一个集合中动态取,可以接下来改变它们的值
holder.shopName =
(TextView) aConvertView.findViewById(R.id.shoppingName);
holder.checkBox =
(CheckBox) aConvertView.findViewById(R.id.checkBox);
} else {
holder = (ViewHolder) aConvertView.getTag();
}
ShopInfoSerialzable shopInfoSerialzable = mShopInfos.get(aPosition);
if (shopInfoSerialzable != null) {
holder.shopName.setText(shopInfoSerialzable.getmShopName()
+ "(" + shopInfoSerialzable.getmShopNumber() + ")");
int finishId = shopInfoSerialzable.getmFinishId();
String comleteStr = (finishId == 1)
? mContext.getResources().getString(R.string.complete)
: mContext.getResources().getString(R.string.uncomplete);
boolean checked = (finishId == 1) ? true : false;
holder.complete.setText(comleteStr);
holder.checkBox.setChecked(checked);
if (checked) {
aConvertView
.setBackgroundColor(Color.parseColor("#99CC66"));
holder.checkBox.setVisibility(CheckBox.INVISIBLE);
} else {
aConvertView.setBackgroundColor(Color.WHITE);
holder.checkBox.setVisibility(CheckBox.VISIBLE);
holder.complete.setTextColor(Color.parseColor("#cc0000"));
}
}
holder.checkBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View av) {
ShopInfoSerialzable shopInfoSerialzable =
mShopInfos.get(aPosition);
int finishId = shopInfoSerialzable.getmFinishId();
if (finishId == 0) { //unCheck to check
String shopName = shopInfoSerialzable.getmShopName();
mDb.updateDataStatus(DataModel.TABLE_SHOPPING,
DataModel.FINISHID, 1,
DataModel.SHOPNAME, shopName);
reGroupStatus(1, shopName);
MyAdapter.this.notifyDataSetChanged();
} else if (finishId == 1) { //check to unCheck
mShoppingName = shopInfoSerialzable.getmShopName();
mDb.updateDataStatus(DataModel.TABLE_SHOPPING,
DataModel.FINISHID, 0, DataModel.SHOPNAME,
mShoppingName);
reGroupStatus(0, mShoppingName);
MyAdapter.this.notifyDataSetChanged();
}
}
});
return aConvertView;
}
显示图片的方式也可以
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ImageView i;
- if(null==convertView){
- i=new ImageView(PeopleScreen.this);
- i.setScaleType(ScaleType.FIT_CENTER);
- i.setLayoutParams(new GridView.LayoutParams(50, 50));
- }else{
- i=(ImageView)convertView;
- }
- ResolveInfo info=mApps.get(position);
- i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
- return i;
- }
}