content: package com.example.zth.two.adapter;
  
  import android.content.Context;
  import android.graphics.Color;
  import android.text.SpannableString;
  import android.text.Spanned;
  import android.text.style.ForegroundColorSpan;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.TextView;
  
  
  import com.example.zth.two.R;
  
  import java.io.File;
  import java.text.DecimalFormat;
  import java.util.List;
  
  /**
   * Created by HXL on 16/3/14.
   */
  public class FileListAdapter extends BaseAdapter {
      Context context;
      List<File> list;
  
      public FileListAdapter(Context context, List<File> list) {
          this.context = context;
          this.list = list;
      }
  
      @Override
      public int getCount() {
          return list.size();
      }
  
      @Override
      public Object getItem(int position) {
          return list.get(position);
      }
  
      @Override
      public long getItemId(int position) {
          return position;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          ViewHolder viewHolder;
          if (convertView == null) {
              viewHolder = new ViewHolder();
              convertView = LayoutInflater.from(context).inflate(R.layout.adapter_file_list, null);
              convertView.setTag(viewHolder);
              viewHolder.name = (TextView) convertView.findViewById(R.id.adapter_file_list_name);
              viewHolder.size = (TextView) convertView.findViewById(R.id.adapter_file_list_create_size);
          } else {
              viewHolder = (ViewHolder) convertView.getTag();
          }
          viewHolder.name.setText(list.get(position).getName());
          viewHolder.size.setText(FormetFileSize(list.get(position).length()));
  
          return convertView;
      }
  
      public String FormetFileSize(long fileS) {// 转换文件大小
          DecimalFormat df = new DecimalFormat("#.00");
          String fileSizeString = "";
          if (fileS < 1024) {
              fileSizeString = df.format((double) fileS) + "B";
          } else if (fileS < 1048576) {
              fileSizeString = df.format((double) fileS / 1024) + "K";
          } else if (fileS < 1073741824) {
              fileSizeString = df.format((double) fileS / 1048576) + "M";
          } else {
              fileSizeString = df.format((double) fileS / 1073741824) + "G";
          }
          return fileSizeString;
      }
  
      static class ViewHolder {
          TextView name;
          TextView size;
      }
  }
  