Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 中自定義 Adapter

Android 中自定義 Adapter

編輯:關於Android編程

public classMyListViewAdapter extendsBaseAdapter {
   
    privateContext mContext= null;
    privateLayoutInflater mLayoutInflater= null;
    privateList<Map<String, Object>> mList= null;
   
    //構造方法,參數list傳遞的就是這一組數據的信息
    publicMyListViewAdapter(Context context, List<Map<String, Object>> list) {
       this.mList= list;
       this.mContext= context;
       this.mLayoutInflater= LayoutInflater.from(mContext);
    }
 
    //得到總的數量
    @Override
    public intgetCount() {
       // TODOAuto-generated method stub
       return this.mList!= null? this.mList.size() : 0;
    }
 
    //根據位置得到View對象
    @Override
    publicObject getItem(intposition) {
       // TODOAuto-generated method stub
       return this.mList.get(position);
    }
 
     //根據ListView位置得到List中的ID
    @Override
    public longgetItemId(intposition) {
       // TODOAuto-generated method stub
       returnposition;
    }
 
    //根據位置得到View對象
    @Override
    publicView getView(intposition, View convertView, ViewGroup parent) {
       // TODOAuto-generated method stub
       //得到條目中的子組件
       ViewHolder holder = null;
       if(convertView == null) {
           holder = newViewHolder();
           convertView = mLayoutInflater.inflate(R.layout.activity_main_item, null);
           holder.imageHead= (ImageView) convertView.findViewById(R.id.imageHead);
           holder.name= (TextView) convertView.findViewById(R.id.name);
           holder.time= (TextView) convertView.findViewById(R.id.time);
           holder.content= (TextView) convertView.findViewById(R.id.content);
           holder.imageContent= (ImageView) convertView.findViewById(R.id.imageContent);
           holder.transmit= (TextView) convertView.findViewById(R.id.transmit);
           holder.comment= (TextView) convertView.findViewById(R.id.comment);
           convertView.setTag(holder);
       } else{
           holder = (ViewHolder) convertView.getTag();
       }
      
       //對ListView的Item中的控件的操作
       holder.imageHead.setImageResource((Integer) mList.get(position).get("imageHead"));
       holder.name.setText(mList.get(position).get("name").toString());
       holder.time.setText(mList.get(position).get("time").toString());
       holder.content.setText(mList.get(position).get("content").toString());
       holder.imageContent.setImageResource((Integer) mList.get(position).get("imageContent"));
       holder.transmit.setText(mList.get(position).get("transmit").toString());
       holder.comment.setText(mList.get(position).get("comment").toString());
      
       returnconvertView;
    }  www.2cto.com
   
   // 定義一個內部類來管理條目中的子組件​
    public final classViewHolder {
       publicImageView imageHead= null;
       publicImageView imageContent= null;
       publicTextView time= null;
       publicTextView name= null;
       publicTextView content= null;
       publicTextView comment= null;
       publicTextView transmit= null;
    }
 
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved