Android版Mono:显示文件选择器? - c#

我正在尝试使用this tutorial将代码从Mono for Android移植到C#。这是我的代码:

FileLayout.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_width="fill_parent">
    <TextView
        android:text="@+id/TextView01"
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:textStyle="bold"
        android:layout_marginTop="5dip"
        android:layout_marginLeft="5dip" />
    <TextView
        android:text="@+id/TextView02"
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip" />
</LinearLayout>

FileArrayAdapter.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace FileChooser
{
    public class FileArrayAdapter : ArrayAdapter<Option>
    {

        private Context c;
        private int id;
        private List<Option> items;

        public FileArrayAdapter(Context context, int textViewResourceId,
                List<Option> objects)
            : base(context, textViewResourceId, objects)
        {

            c = context;
            id = textViewResourceId;
            items = objects;
        }

        public Option getItem(int i)
        {
            return items[i];
        }
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View v = convertView;
            if (v == null)
            {
                LayoutInflater vi = (LayoutInflater)c.GetSystemService(Context.LayoutInflaterService);
                v = vi.Inflate(id, null);
            }
            Option o = items[position];
            if (o != null)
            {
                TextView t1 = (TextView)v.FindViewById(Resource.Id.TextView01);
                TextView t2 = (TextView)v.FindViewById(Resource.Id.TextView02);

                if (t1 != null)
                    t1.Text = o.getName().ToString();
                if (t2 != null)
                    t2.Text = o.getData().ToString();

            }
            return v;
        }


    }

}

Activity1.cs:

using System;
using System.Collections;
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Android.OS;
using Java.IO;
using Environment = Android.OS.Environment;


namespace FileChooser
{
    [Activity(Label = "FileChooser", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : ListActivity
    {
        private File currentDir;
        private FileArrayAdapter adapter;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            currentDir = new File(Environment.RootDirectory.AbsolutePath);
            fill(currentDir);

        }

        private void fill(File f)
        {
            File[] dirs = f.ListFiles();

            Title = ("Current Dir: " + f.Name);
            List<Option> dir = new List<Option>();
            List<Option> fls = new List<Option>();

            try
            {
                foreach (var ff in dirs)
                    if (ff.IsDirectory)
                        dir.Add(new Option(ff.Name, "Folder", ff.AbsolutePath));
                    else
                        fls.Add(new Option(ff.Name, "File Size: " + ff.Length(), ff.AbsolutePath));


                dir.AddRange(fls);

                if (!String.Equals(f.Name, "sdcard", comparisonType: StringComparison.InvariantCultureIgnoreCase))
                    dir.Insert(0, new Option("..", "Parent Directory", f.Parent));

                adapter = new FileArrayAdapter(this, Resource.Layout.FileLayout, dir);
                this.ListAdapter = adapter;

            } 
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Error: ", e.Message);

            }


        }



    }
}

我收到以下错误:

The program 'Mono' has exited with code 255 (0xff).

我在最后一句话之后加上了一个断点。这是我的局部变量的截图:

参考方案

我可能不在这里,但是前段时间我写了一个简单的轻量级文件选择小部件。

package com.skype.widget;
import java.io.File;
import java.util.ArrayList;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * General purpose, light-weight file explorer widget.<br>
 * Upon initialization, it will point at the root of the filesystem, at which
 * point you can manipulate it with {@link #browseToLocation(String)}
 * 
 * @author Aleksandar Milenkovic
 */

public class FileBrowser extends ListView implements AdapterView.OnItemClickListener
{
/**
 * Use display mode to toggle between absolute and relative display modes.
 */
public enum DISPLAYMODE
{
    ABSOLUTE, RELATIVE;
}

/**
 * Use work mode to toggle between folder-only and files-and-folders mode.
 */
public enum WORKMODE
{
    FOLDER_ONLY, FILE_AND_FOLDER;
}

public static final String      ROOT        = "/";
public static final String      LOGTAG      = "FileBrowser";

@SuppressWarnings("unused")
private Context                 context;

private final DISPLAYMODE       displayMode = DISPLAYMODE.RELATIVE;
private WORKMODE                workMode    = WORKMODE.FILE_AND_FOLDER;
private ArrayList<String>       directoryEntries;
private File                    currentDirectory;
private ArrayAdapter<String>    mAdapter;


public FileBrowser(Context context)
{
    this(context, null, 0);
}

public FileBrowser(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    this.context = context;
    //init members
    directoryEntries = new ArrayList<String>();
    mAdapter = new ArrayAdapter<String>(context, R.layout.file_browser_entry, this.directoryEntries);
    this.setAdapter(mAdapter);

    //browse to root
    browseToLocation(ROOT);

    //refresh UI elements
    mAdapter.notifyDataSetChanged();
    setOnItemClickListener(this);
}

public FileBrowser(Context context, AttributeSet attrs)
{
    this(context, attrs, 0);
}


/**
 * This function browses to the root-directory of the file-system.
 * 
 * @param location
 *            the absolute path you want to browse to.
 */
public void browseToLocation(String location)
{
    browseTo(new File(location));
}

/**
 * This function browses up one level according to the field:
 * currentDirectory
 */
private void upOneLevel()
{
    Log.d(LOGTAG, "upOneLevel()");
    if (currentDirectory.getAbsolutePath() == ROOT) return;
    if (this.currentDirectory.getParent() != null) this.browseTo(this.currentDirectory.getParentFile());
}

private void browseTo(final File aDirectory)
{
    Log.d(LOGTAG, "browseTo()");
    if (aDirectory.isDirectory())
    {
        this.currentDirectory = aDirectory;
        fill(aDirectory.listFiles());
    } else
    {
        Log.d(LOGTAG, aDirectory.getAbsolutePath());
    }
}

private void fill(File[] files)
{
    Log.d(LOGTAG, "fill()");
    this.directoryEntries.clear();

    // Add the "." and the ".." == 'Up one level'
    this.directoryEntries.add(".");
    if (this.currentDirectory.getParent() != null) this.directoryEntries.add("..");

    switch (this.workMode)
    {
        case FOLDER_ONLY:
            switch (this.displayMode)
            {
                case ABSOLUTE:
                    for (File file : files)
                    {
                        if (file.isDirectory()) this.directoryEntries.add(file.getPath() + "/");
                    }
                    break;
                case RELATIVE: // On relative Mode, we have to add the current-path to the beginning
                    int currentPathStringLenght = this.currentDirectory.getAbsolutePath().length();
                    for (File file : files)
                    {
                        if (file.isDirectory())
                            this.directoryEntries.add(file.getAbsolutePath().substring(currentPathStringLenght) + "/");
                    }
                    break;
            }
            break;

        case FILE_AND_FOLDER:
            switch (this.displayMode)
            {
                case ABSOLUTE:
                    for (File file : files)
                    {
                        if (file.isDirectory())
                            this.directoryEntries.add(file.getPath() + "/");
                        else
                            this.directoryEntries.add(file.getPath());
                    }
                    break;
                case RELATIVE: // On relative Mode, we have to add the current-path to the beginning
                    int currentPathStringLenght = this.currentDirectory.getAbsolutePath().length();
                    for (File file : files)
                    {
                        if (file.isDirectory())
                            this.directoryEntries.add(file.getAbsolutePath().substring(currentPathStringLenght) + "/");
                        else
                            this.directoryEntries.add(file.getAbsolutePath().substring(currentPathStringLenght));
                    }
                    break;
            }
    }

}

/**
 * Internal onItemClick method that handles browsing and displaying.<br>
 * <b>Make sure to call this at the start of the outer onItemClick.</b>
 */
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
    String selectedFileString = directoryEntries.get(arg2);
    Log.d(LOGTAG, "onItemClick() fired!");
    if (selectedFileString.equals("."))
    {
        // Refresh
        browseTo(currentDirectory);
    } else if (selectedFileString.equals(".."))
    {
        upOneLevel();
    } else
    {
        File clickedFile = null;
        switch (displayMode)
        {
            case RELATIVE:
                clickedFile = new File(currentDirectory.getAbsolutePath() + directoryEntries.get(arg2));
                break;
            case ABSOLUTE:
                clickedFile = new File(directoryEntries.get(arg2));
                break;
        }
        if (clickedFile != null) browseTo(clickedFile);
    }
    mAdapter.notifyDataSetChanged();
    this.postInvalidate();
}

/**
 * Gets the current path in absolute form.
 * 
 * @return the absolute path pointing to the current directory.
 */
public String getCurrentPath()
{
    return currentDirectory.getAbsolutePath();
}

/**
 * Method used for setting the work mode of this widget. <br>
 * It can be either {@link WORKMODE#FILE_AND_FOLDER} or {@link WORKMODE#FOLDER_ONLY}
 * @param wm
 * the workmode to use.
 */
public void setWorkMode(WORKMODE wm)
{
    this.workMode = wm;
}

/**
 * Returns the current workmode of this widget.
 * @return
 * {@link #workMode} as {@link WORKMODE} enum
 */
public WORKMODE getWorkMode()
{
    return workMode;
}

}

用法:

 final FileBrowser fb = (FileBrowser) dialog.findViewById(R.id.folder_list);
    fb.setWorkMode(FileBrowser.WORKMODE.FOLDER_ONLY);
    fb.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
        {
            fb.onItemClick(arg0, arg1, arg2, arg3);

            txt.setText(fb.getCurrentPath());
            savingPath = fb.getCurrentPath() + "/";
        }
    });

抱歉,如果那不是您想要的。

Android Firebase:将数据保存到数据库 - java

我正在尝试从Firebase保存和检索数据,但是在获取正确的语法时遇到了一些麻烦。用户通过电子邮件和密码的身份验证方法进行注册,并使用相同的详细信息登录。我有两个编辑文本字段,要求输入名称和语句。在两个编辑文本框下面有一个按钮,用于将数据保存到数据库。private void saveQuote(){ String name = author.getText…

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

改造正在返回一个空的响应主体 - java

我正在尝试使用Retrofit和Gson解析一些JSON。但是,我得到的响应机构是空的。当我尝试从对象中打印信息时,出现NullPointerException。我确保URL正确,并且我也确保POJO也正确。我正在使用jsonschema2pojo来帮助创建POJO类。这是我要解析的JSON{ "?xml": { "@versi…

获取PHP中函数调用者的__FILE__常量 - php

我知道PHP中的__FILE__魔术常数将变成当前执行文件的完整路径和文件名。但是,有什么方法可以使函数的调用文件获得相同的信息吗?例如://foo.php: include "bar.php"; call_it(); //bar.php function call_it() { echo "Calling file: …

每个文件合并后添加换行 - python

我有很多类似以下内容的JSON文件:例如。1.json{"name": "one", "description": "testDescription...", "comment": ""} test.json{"name"…