Android 中自定义 Adapter

public classMyListViewAdapter extendsBaseAdapter {

    

    privateContext mContextnull;

    privateLayoutInflater mLayoutInflaternull;

    privateList<Map<String, Object>> mListnull;

    

    //构造方法,参数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!= nullthis.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_itemnull);

           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();

       }

       

       //ListViewItem中的控件的操作

       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;

    }

    

   // 定义一个内部类来管理条目中的子组件

    public final classViewHolder {

       publicImageView imageHeadnull;

       publicImageView imageContentnull;

       publicTextView timenull;

       publicTextView namenull;

       publicTextView contentnull;

       publicTextView commentnull;

       publicTextView transmitnull;

    }

 

}

 

 

你可能感兴趣的:(android,android)