首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >xamarin.Android C# - IFileNameFilter类实现问题

xamarin.Android C# - IFileNameFilter类实现问题
EN

Stack Overflow用户
提问于 2015-04-10 14:07:39
回答 2查看 231关注 0票数 1

在我的“文件资源管理器”应用程序中,我尝试在任何文件夹中只显示xml文件--每次打开文件夹时,我只想显示present.So文件。我查看过这个站点和互联网,有很多示例,但在C#中没有找到。我还尝试修改其他示例,以满足我的需要,但我无法实现IFileNameFilter class.Does --有人知道怎么做吗?我试着实现它,但不断得到构建错误。

在下面的代码中,我有一个ListFragment类,它显示文件:

代码语言:javascript
复制
public override void OnListItemClick(ListView l, View v, int position, long id)
    {

        try{

        var fileSystemInfo = _adapter.GetItem(position);


        if (fileSystemInfo.IsFile())
        {
            // Do something with the file.  In this case we just pop some toast.
            Log.Verbose("FileListFragment", "The file {0} was clicked.", fileSystemInfo.FullName);
            Toast.MakeText(Activity, "You selected file " + fileSystemInfo.FullName, ToastLength.Short).Show();

            OnFileClick (fileSystemInfo);
        }
        else
        {
            // Dig into this directory, and display it's contents

                RefreshFilesList(fileSystemInfo.FullName);
        }

        }catch(Exception e){

            Log.Error (TAG,e.ToString());

        }

        base.OnListItemClick(l, v, position, id);


    }

public void RefreshFilesList(string directory)
    {

        //GenericExtFilter filter = new GenericExtFilter(extension);

        IList<FileSystemInfo> visibleThings = new List<FileSystemInfo>();

        var dir = new DirectoryInfo(directory);

        try
        {
            foreach (var item in dir.GetFileSystemInfos().Where(item => item.IsVisible()))
            {

                visibleThings.Add(item);

            }
        }
        catch (Exception ex)
        {
            Log.Error("FileListFragment", "Couldn't access the directory " + _directory.FullName + "; " + ex);
            Toast.MakeText(Activity, "Problem retrieving contents of " + directory, ToastLength.Long).Show();
            return;
        }

        _directory = dir;

        _adapter.AddDirectoryContents(visibleThings);

        // If we don't do this, then the ListView will not update itself when the data set 
        // in the adapter changes. It will appear to the user that nothing has happened.
        ListView.RefreshDrawableState();

        Log.Verbose("FileListFragment", "Displaying the contents of directory {0}.", directory);
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-15 11:06:57

我用一个简单得多的解决方案解决了这个问题,我没有在上面的IFileNameFilter ( all.Inside,RefreshFilesList方法)之后使用它,我做了以下修改:

代码语言:javascript
复制
foreach (var item in dir.GetFileSystemInfos().Where(item => item.IsVisible()))
            {
                if(item.IsDirectory())
                {

                    visibleThings.Add(item);

                }else if(item.IsFile())
                {

                    bool isXmlFile = item.Extension.ToLower().EndsWith("xml");
                    if(isXmlFile)
                    {
                    visibleThings.Add(item);
                    }

                }
            }

这现在显示了所有文件夹和存在的任何xml文件。

票数 -1
EN

Stack Overflow用户

发布于 2015-04-11 04:16:52

如果我正确理解,您希望创建将过滤IFilenameFilter文件的XML。

代码语言:javascript
复制
public class XmlFileFilter : Java.Lang.Object, IFilenameFilter
{
    public bool Accept(File dir, string filename)
    {
        return filename.ToLower().EndsWith(".xml");
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29563592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档