显示使用FilenameFilter加载的文件的友好名称

时间:2013-06-13 10:29:04

标签: java android

我在sdcard上的目录只有四个文件如下:

sdcard/myfolder/15052013/15052013.tpm
sdcard/myfolder/loek/loek.doc
sdcard/myfolder/Android/Android.txt
sdcard/myfolder/Input_Output/Input_Output.htm

我使用以下代码从目录中获取两个特定文件(loekInput_Output),然后在对话框中显示文件名:

button = (ImageButton)findViewById(R.id.swap);
button.setOnClickListener(new View.OnClickListener() {          
        @Override
        public void onClick(View v) {               
            loadFileList();
            showDialog(DIALOG_LOAD_FILE);
            }
    }); 

private void loadFileList() {
       if(mPath.exists()) {           
        FilenameFilter filter = new FilenameFilter() {
            public boolean accept(File dir, String filename) {                
                return !filename.startsWith("15052013") && !filename.startsWith("Android");                            
            }
        };        
        mFileList = mPath.list(filter);           
    }
    else 
    {
        mFileList= new String[0];
    }
}

@Override
protected Dialog onCreateDialog(int id) {
    Dialog dialog = null;
    AlertDialog.Builder builder = new Builder(this);
    switch(id) {
        case DIALOG_LOAD_FILE:
            builder.setTitle("Choose one");
            builder.setIcon(R.drawable.dictionary);
            if(mFileList == null) {
                Log.e(MAIN_TAG, "Showing file picker before loading the file list");
                dialog = builder.create();                                         
                return dialog;
            }
            builder.setItems(mFileList, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    mDBFile = mDBList.items.get(which);                     
                    setContentView(R.layout.main);
                    menuMain();                     
                }
            });
            break;
    }    
    dialog = builder.show();
    dialog.getWindow().setGravity(Gravity.TOP);
    android.view.WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
    params.y = 90;
    //params.x = 0;
    params.height = 500;
    params.width = 400;
    params.dimAmount=0.f;
    dialog.getWindow().setAttributes(params);

    return dialog;      
}

文件名已成功加载并显示在对话框中,但它们都是“原始”文件名。我希望文件名看起来像:

loek ==> Information about Mr Loek Basten
Input_Output ==> Differences between the In-Flow and Out-Flow

现在谈谈我的问题:如何为这些文件显示更友好的名称?

我想知道您是否可以帮助我的代码?非常感谢你。

1 个答案:

答案 0 :(得分:0)

一种解决方案是创建一个ResourceBundle,其中属性文件如下所示:

loek.doc=Information about Mr Loek Basten
Input_Output.htm=Differences between the In-Flow and Out-Flow

这样您就可以在不更改源代码的情况下添加更多文件及其描述。