`
anson_xu
  • 浏览: 501260 次
  • 性别: Icon_minigender_1
  • 来自: 惠州
社区版块
存档分类

自定义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;
  }

 

 

 

显示图片的方式也可以

  1. @Override  
  2.         public View getView(int position, View convertView, ViewGroup parent) {   
  3.             ImageView i;   
  4.             if(null==convertView){   
  5.                 i=new ImageView(PeopleScreen.this);   
  6.                 i.setScaleType(ScaleType.FIT_CENTER);   
  7.                 i.setLayoutParams(new GridView.LayoutParams(5050));   
  8.             }else{   
  9.                 i=(ImageView)convertView;   
  10.             }   
  11.             ResolveInfo info=mApps.get(position);   
  12.             i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));   
  13.             return i;   
  14.         }   


 }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics