https://blog.csdn.net/trbbadboy/article/details/7899424 和 https://blog.csdn.net/yuanyuan_186/article/details/40709901 的博客中都是用了 Activity的onCreateDialog 及dialogID
而本文的原則: 捨棄使用 Activity的onCreateDialog 及dialogID;setDlg的方式,直接dlg.dimiss實現。
public class OpenFileDialog {
public static String tag = "OpenFileDialog";
static final public String sRoot = "/sdcard";
static final public String sParent = "..";
static final public String sFolder = ".";
static final public String sEmpty = "";
static final private String sOnErrorMsg = "No rights to access!";
// 參數說明
// context:上下文
// title:對話框標題
// callback:一個傳遞Bundle參數的回調接口
// suffix:需要選擇的文件後綴,比如需要選擇wav、mp3文件的時候設置爲".wav;.mp3;",注意最後需要一個分號(;)
// images:用來根據後綴顯示的圖標資源ID。
// 根目錄圖標的索引爲sRoot;
// 父目錄的索引爲sParent;
// 文件夾的索引爲sFolder;
// 默認圖標的索引爲sEmpty;
// 其他的直接根據後綴進行索引,比如.wav文件圖標的索引爲"wav"
public static Dialog createDialog(Context context, String title, CallbackBundle callback, String suffix, Map<String, Integer> images){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
FileSelectView fv = new FileSelectView(context, callback, suffix, images);
builder.setView(fv);
Dialog dialog = builder.create();
//dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
fv.setDlg(dialog);
dialog.setTitle(title);
return dialog;
}
static class FileSelectView extends ListView implements OnItemClickListener{
private CallbackBundle callback = null;
private String path = sRoot;
private List<Map<String, Object>> list = null;
Dialog dlg = null;
private String suffix = null;
private Map<String, Integer> imagemap = null;
public FileSelectView(Context context, CallbackBundle callback, String suffix, Map<String, Integer> images) {
super(context);
this.imagemap = images;
this.suffix = suffix==null?"":suffix.toLowerCase();
this.callback = callback;
this.setOnItemClickListener(this);
refreshFileList();
}
public void setDlg(Dialog dialog){
dlg = dialog;
}
private String getSuffix(String filename){
int dix = filename.lastIndexOf('.');
if(dix<0){
return "";
}
else{
return filename.substring(dix+1);
}
}
private int getImageId(String s){
if(imagemap == null){
return 0;
}
else if(imagemap.containsKey(s)){
return imagemap.get(s);
}
else if(imagemap.containsKey(sEmpty)){
return imagemap.get(sEmpty);
}
else {
return 0;
}
}
private int refreshFileList(){
// 刷新文件列表
File[] files = null;
try{
files = new File(path).listFiles();
}
catch(Exception e){
files = null;
}
if(files==null){
// 訪問出錯
Toast.makeText(getContext(), sOnErrorMsg,Toast.LENGTH_SHORT).show();
return -1;
}
if(list != null){
list.clear();
}
else{
list = new ArrayList<Map<String, Object>>(files.length);
}
// 用來先保存文件夾和文件夾的兩個列表
ArrayList<Map<String, Object>> lfolders = new ArrayList<Map<String, Object>>();
ArrayList<Map<String, Object>> lfiles = new ArrayList<Map<String, Object>>();
if(!this.path.equals(sRoot)){
// 添加根目錄 和 上一層目錄
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", sRoot);
map.put("path", sRoot);
map.put("img", getImageId(sRoot));
list.add(map);
map = new HashMap<String, Object>();
map.put("name", sParent);
map.put("path", path);
map.put("img", getImageId(sParent));
list.add(map);
}
for(File file: files)
{
if(file.isDirectory() && file.listFiles()!=null){
// 添加文件夾
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", file.getName());
map.put("path", file.getPath());
map.put("img", getImageId(sFolder));
lfolders.add(map);
}
else if(file.isFile()){
// 添加文件
String sf = getSuffix(file.getName()).toLowerCase();
if(suffix == null || suffix.length()==0 || (sf.length()>0 && suffix.indexOf("."+sf+";")>=0)){
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", file.getName());
map.put("path", file.getPath());
map.put("img", getImageId(sf));
lfiles.add(map);
}
}
}
list.addAll(lfolders); // 先添加文件夾,確保文件夾顯示在上面
list.addAll(lfiles); //再添加文件
SimpleAdapter adapter = new SimpleAdapter(getContext(), list, R.layout.filedialogitem, new String[]{"img", "name", "path"}, new int[]{R.id.filedialogitem_img, R.id.filedialogitem_name, R.id.filedialogitem_path});
this.setAdapter(adapter);
return files.length;
}
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// 條目選擇
String pt = (String) list.get(position).get("path");
String fn = (String) list.get(position).get("name");
if(fn.equals(sRoot) || fn.equals(sParent)){
// 如果是更目錄或者上一層
File fl = new File(pt);
String ppt = fl.getParent();
if(ppt != null){
// 返回上一層
path = ppt;
}
else{
// 返回更目錄
path = sRoot;
}
}
else{
File fl = new File(pt);
if(fl.isFile()){
// 如果是文件
//((Activity)getContext()).dismissDialog(this.dialogid); // 讓文件夾對話框消失
if(dlg != null) {
dlg.dismiss();
}
// 設置回調的返回值
Bundle bundle = new Bundle();
bundle.putString("path", pt);
bundle.putString("name", fn);
// 調用事先設置的回調函數
this.callback.callback(bundle);
return;
}
else if(fl.isDirectory()){
// 如果是文件夾
// 那麼進入選中的文件夾
path = pt;
}
}
this.refreshFileList();
}
}
}
res/layout xml 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/vw1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="horizontal"
android:padding="4dp" >
<ImageView
android:id="@+id/filedialogitem_img"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="4dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/filedialogitem_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/filedialogitem_path"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textColor="#FFFFFF"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>