BaseAdapter

BaseAdapter 需要自己去实现

class  ItemAttr {
    public int itemImageResId;//图像资源ID
    public String itemTitle;   //标题
    public String itemContent; //内容
    public ItemAttr(int itemImageResId,String itemTitle,String itemContent) {
        this.itemImageResId = itemImageResId;
        this.itemTitle = itemTitle;
        this.itemContent = itemContent;
    }
}
public class MyBaseAdapter extends BaseAdapter {
    private List mList;
    private LayoutInflater mInflater;
    public MyBaseAdapter(Context context, List list) {
            this.mList = list;
            this.mInflater = LayoutInflater.from(context);
    }

    @Override 
  //返回当前list 的总数  一定要有
    public int getCount() {
        return mList.size();
    }

    @Override
// 返回当前处理对象
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    //指定索引对应的数据ID
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (viewHolder == null) {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item,null);
            viewHolder.txTitle  = (TextView)convertView.findViewById(R.id.tx_title);
            viewHolder.imageView = (ImageView)convertView.findViewById(R.id.imageView);
            viewHolder.txContent = (TextView)convertView.findViewById(R.id.tx_content);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        ItemAttr itemAttr = mList.get(position);
        viewHolder.txTitle.setText(itemAttr.itemTitle);
        viewHolder.imageView = (ImageView)convertView.findViewById(R.id.imageView);
        viewHolder.txContent.setText(itemAttr.itemContent);
        return convertView;
    }

    static class ViewHolder {
        ImageView imageView;
        TextView txTitle;
        TextView txContent;
    }
  • class ViewHold 和 class ItemAttr 定义的为xml 布局里面的空间

xml



    
    
    

你可能感兴趣的:(BaseAdapter)