所以这是我问过的上一个问题的后续步骤Here
在遵循Jasons的建议之后,我做了一点点恢复,以使用System.IO来使用listview在android上显示文件夹。
我从Microsoft网站获得以下示例...
class PublicListViews : ListActivity
{
public string ListCreate(string path)
{
ListAdapter = new ArrayAdapter<string>(this, Resource.Layout.list_item, countries);
ListView.TextFilterEnabled = true;
ListView.ItemClick += delegate (object sender, AdapterView.ItemClickEventArgs args)
{
Toast.MakeText(Application, ((TextView)args.View).Text, ToastLength.Short).Show();
};
return path;
}
}
根据我对这行的理解。...(这是Resource.Layout.list_item,国家/地区;将在android项目的resourses文件夹中使用list_item.xml,countrys是将要列出的国家的数组。
而且我还找到了以下代码来获取目录和文件...
public static void ProcessDirectory(string targetDirectory)
{
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(targetDirectory);
foreach (string fileName in fileEntries)
ProcessFile(fileName);
// Recurse into subdirectories of this directory.
string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach (string subdirectory in subdirectoryEntries)
ProcessDirectory(subdirectory);
}
// Insert logic for processing found files here.
public static void ProcessFile(string path)
{
Console.WriteLine("Processed file '{0}'.", path);
}
有人可以帮助我实现使用ListAdaptor列出文件夹/文件以及如何通过MainPage.xaml进行调用吗?
Mark。
根据您的描述,我想您想从文件夹中获取文件,然后在xamarin.android中的ListView中显示这些文件路径?我说的对吗?
如果是的话,我创建一个简单的例子,您可以看一下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="get files" android:id="@+id/button1"/>
</LinearLayout>
public class MainActivity : AppCompatActivity
{
private ListView listview1;
private Button button1;
private List<string> files;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
listview1 = FindViewById<ListView>(Resource.Id.listView1);
button1 = FindViewById<Button>(Resource.Id.button1);
files = new List<string>();
button1.Click += delegate
{
getpermission();
//var targetdic = global::Android.OS.Environment.ExternalStorageDirectory.Path + "/testfolder";
var targetdic = Android.OS.Environment.ExternalStorageDirectory + Java.IO.File.Separator + "testfolder";
if(Directory.Exists(targetdic))
{
files = DirSearch(targetdic);
listview1.Adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, files);
listview1.ItemClick += (s, e) =>
{
var t = files[e.Position];
Android.Widget.Toast.MakeText(this, t, Android.Widget.ToastLength.Long).Show();
};
}
};
}
private void getpermission()
{
if (ActivityCompat.CheckSelfPermission(this, Android.Manifest.Permission.ReadExternalStorage) != Android.Content.PM.Permission.Granted)
{
ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.ReadExternalStorage }, 1);
return;
}
}
private List<String> DirSearch(string sDir)
{
List<string> folders = new List<string>();
try
{
foreach (string f in Directory.GetFiles(sDir))
{
folders.Add(f);
}
foreach (string d in Directory.GetDirectories(sDir))
{
folders.AddRange(DirSearch(d));
}
}
catch (System.Exception excpt)
{
}
return folders;
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
请不要忘记请求READ_EXTERNAL_STORAGE许可