Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android C#-IFileNameFilter类实现问题_C#_Android_Xamarin_Xamarin.android - Fatal编程技术网

Android C#-IFileNameFilter类实现问题

Android C#-IFileNameFilter类实现问题,c#,android,xamarin,xamarin.android,C#,Android,Xamarin,Xamarin.android,在我的文件资源管理器应用程序中,我试图只显示存在的任何文件夹中的xml文件。因此,每次打开文件夹时,我只想展示xml文件。我浏览了这个网站和互联网,有很多示例,但在C#中没有找到。我也尝试过根据自己的需要修改其他示例,但我一直无法实现IFileNameFilter类。有人知道怎么做吗?我尝试过实现它,但不断出现构建错误 在下面的代码中,我有一个显示文件的ListFragment类: public override void OnListItemClick(ListView l, View v,

在我的文件资源管理器应用程序中,我试图只显示存在的任何文件夹中的xml文件。因此,每次打开文件夹时,我只想展示xml文件。我浏览了这个网站和互联网,有很多示例,但在C#中没有找到。我也尝试过根据自己的需要修改其他示例,但我一直无法实现IFileNameFilter类。有人知道怎么做吗?我尝试过实现它,但不断出现构建错误

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

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);
    }
public override void OnListItemClick(列表视图l、视图v、整数位置、长id)
{
试一试{
var fileSystemInfo=\u adapter.GetItem(位置);
if(fileSystemInfo.IsFile())
{
//对文件做点什么。在这种情况下,我们只需放一些烤面包片。
Verbose(“FileListFragment”,“文件{0}被单击。”,fileSystemInfo.FullName);
Toast.MakeText(活动,“您选择的文件”+fileSystemInfo.FullName,ToastLength.Short).Show();
OnFileClick(文件系统信息);
}
其他的
{
//深入此目录,并显示其内容
刷新文件列表(fileSystemInfo.FullName);
}
}捕获(例外e){
Log.Error(标记,例如ToString());
}
base.OnListItemClick(l、v、位置、id);
}
公共无效刷新文件列表(字符串目录)
{
//GenericExtFilter=新的GenericExtFilter(扩展名);
IList visibleThings=新列表();
var dir=新目录信息(目录);
尝试
{
foreach(dir.GetFileSystemInfos()中的var项,其中(item=>item.IsVisible())
{
可见事物。添加(项);
}
}
捕获(例外情况除外)
{
Log.Error(“FileListFragment”,“无法访问目录”+_directory.FullName+“;”+ex);
Toast.MakeText(活动,“检索“+目录的内容时出现问题,ToastLength.Long).Show();
返回;
}
_directory=dir;
_adapter.AddDirectoryContents(visibleThings);
//如果我们不这样做,那么当数据集
//在中,适配器发生了更改。用户会觉得什么都没有发生。
ListView.RefreshDrawableState();
Verbose(“FileListFragment”,“显示目录{0}.”的内容,目录);
}

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

public class XmlFileFilter : Java.Lang.Object, IFilenameFilter
{
    public bool Accept(File dir, string filename)
    {
        return filename.ToLower().EndsWith(".xml");
    }
}

我用了一个非常简化的解决方案解决了这个问题。毕竟我没有使用IFileNameFilter。在上面的RefreshFileList方法中,我做了以下更改:

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文件。

,谢谢你的回答,但也许我应该说得更具体一些。你在这里添加的代码片段并不是我正在努力解决的问题。我现在在实现上有点困难的部分是如何在我上面的代码中使用这个类XmlFileFilter。我想我需要在我向visibleThings列表。我已经尝试了一些事情,比如,在foreach循环中添加一个if语句,在将扩展添加到循环之前检查扩展,但这不正常。它不添加目录。