文件下载2

MD不知道怎么回事无法编辑随笔,可能我的一篇随笔上传的代码太多了

更改下最后的一个类的解释不是删除是查看,因为打开pdf和doc等其他文件的方式不一样

OK继续。

DownloadedObjectFactory将以后所有需要的类型统一生成,只需要在构造中将类型加入。

package com.hsm.factory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hsm.downloadedfile.DownloadedPDF;
import com.hsm.modelinterface.DownloadedInterface;

public class DownloadedObjectFactory
{
  private List<Map<String, Object>> list;
  private static DownloadedObjectFactory downloadedPDFFactory;

  private DownloadedObjectFactory()
  {
    list = new ArrayList<Map<String, Object>>();
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("pdf", new DownloadedPDF("pdf"));
    list.add(map);
  }

  public static DownloadedObjectFactory getInstance()
  {
    if (downloadedPDFFactory == null)
    {
      downloadedPDFFactory = new DownloadedObjectFactory();
    }
    return downloadedPDFFactory;
  }

  public DownloadedInterface getDownloadObjForName(String foderPath)
  {
    Object object = null;
    for (Map<String, Object> map : list)
    {
      object = map.get(foderPath);
      if (object != null)
      {
        break;
      }
    }
    return (DownloadedInterface) object;
  }
}

 DownloadedDelegat负责c和v之间的通信。

package com.hsm.modelinterface;

public interface DownloadedDelegat
{
  public void DownloadedDelegate(String name , int event);
}

 DownloadedInterface将C层的需求抽象出来形成接口。

package com.hsm.modelinterface;

import java.util.List;

import android.content.Context;

/**
 * 接口负责已下载完成的方法
 * @author hesiming
 *
 */
public interface DownloadedInterface
{
  /**
   * 方法查询目录下的文件
   */
  public List<String> queryDownloadedFiles();
  /**
   * 删除目录下的文件(不需要路径已经在内存中保留了List所以直接循环查询List中的文件然后删除就可以不用考虑文件路径)
   */
  public void removeFileForName(String fileName);
  /**
   * 打开目录下的文件
   */
  public void openDownloadedFile(String fileName , Context context);
}

 最后一个就是Adapter类实现V层的显示。

package com.hsm.view;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hsm.Activity.R;
import com.hsm.modelinterface.DownloadedDelegat;

public class DownloadedAdapter extends BaseAdapter
{
  private DownloadedDelegat delegate;

  public void setDelegate(DownloadedDelegat delegate)
  {
    this.delegate = delegate;
  }

  private Context context;
  /**
   * 找寻Layout下的xml文件
   */
  private LayoutInflater mInflater;
  /**
   * 记录文件名(name)&文件路径(url)
   */
  private List<String> item;

  public DownloadedAdapter(Context context, List<String> item)
  {
    this.item = item;
    this.context = context;
    mInflater = LayoutInflater.from(context);
  }

  @Override
  public int getCount()
  {
    // TODO Auto-generated method stub
    return item.size();
  }

  @Override
  public Object getItem(int position)
  {
    // TODO Auto-generated method stub
    return item.get(position);
  }

  @Override
  public long getItemId(int position)
  {
    // TODO Auto-generated method stub
    return 0;
  }

  @Override
  public View getView(final int position, View convertView, ViewGroup parent)
  {
    // TODO Auto-generated method stub3
    ViewHolder holder;
    if (convertView == null)
    {
      convertView = mInflater.inflate(R.layout.downloaded, null);
      holder = new ViewHolder();
      holder.name = (TextView) convertView.findViewById(R.id.downloaded_name);
      holder.look = (Button) convertView.findViewById(R.id.downloaded_look);
      convertView.setTag(holder);
    }
    else
    {
      holder = (ViewHolder) convertView.getTag();
    }
    holder.name.setText(item.get(position).toString());
    holder.look.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v)
      {
        if (delegate != null)
        {
          delegate.DownloadedDelegate(item.get(position), 2);
        }
      }
    });
    convertView.setOnLongClickListener(new View.OnLongClickListener() {
      public boolean onLongClick(View v)
      {
        new AlertDialog.Builder(context).setTitle("信息").setMessage("是否删除?").setPositiveButton(
            "确定", new DialogInterface.OnClickListener() {
              // 添加删除的按钮
              public void onClick(DialogInterface dialog, int which)
              {
                // TODO Auto-generated method stub
                if (delegate != null)
                {
                  delegate.DownloadedDelegate(item.get(position), 1);
                }
                Toast.makeText(context, "已经删除", 0).show();
              }
            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {// 添加取消删除的按钮选项
              public void onClick(DialogInterface dialog, int which)
              {
                dialog.dismiss();
              }
            }).show();
        return true;
      }
    });
    return convertView;
  }

  /**
   * @brief 绑定viewholder中的控件以减少创建view的时间
   * @author hesiming
   * 
   */
  private class ViewHolder
  {
    ImageView imageView;
    TextView name;
    Button look;
  }
}

 上传模拟器上运行图片

posted @ 2012-01-09 12:33  飞翔的熊猫  阅读(213)  评论(0编辑  收藏  举报