Android 中自訂 Adapter

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.