Android LIstView适配器删除文件和查看项目点击

本文关键字:项目 LIstView 适配器 删除 文件 Android | 更新日期: 2023-09-27 18:18:00

我下面的代码有一个问题。我需要用户能够单击列表中的项目,并有一个菜单弹出,询问他们是否想加载或删除文件。我没有问题得到文件加载,但我不能得到删除功能的工作。现在我需要用户能够点击项目,并有文件删除与列表上的视图一起。我已经尝试了一切,我无法从sd卡中删除文件或从列表视图或视图中删除视图。非常感谢任何帮助。

public class loadActivity : Activity,ListView.IOnItemClickListener
{
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        SetContentView (Resource.Layout.loadhead);
        Button buttonBack = FindViewById<Button> (Resource.Id.btnLoadHeadBack);
        buttonBack.Click += delegate {
            Finish ();
        };
        ListView listView = FindViewById<ListView> (Resource.Id.headList);
        String loadData = myResources.saveLoadData;
        if (loadData == null || loadData == "")
            return;
        String[] splitData = loadData.Substring (0, loadData.Length - 1).Split (''n');
        var Adapter = new ArrayAdapter<String> (this, Android.Resource.Layout.SimpleListItem1, splitData);
        listView.Adapter = Adapter;
        listView.OnItemClickListener = this;

    }
        public void OnItemClick(AdapterView parent, View view, int position, long id)
        {
            //whatever you need it to do goes here.
            var fileName = parent.GetItemAtPosition(position);
            var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.Path;
            var textPath = Path.Combine(sdCardPath, fileName.ToString());
            var load = new Intent (this, typeof(results));
            load.PutExtra("LoadData" , readFileSdcardFile(textPath));
        //  myResources.isLast = false;
        //  myResources.isLoadingNow = true; 
        //  myResources.songLyrics = ""; 
        //  StartActivity (load);
        parent.RemoveViewInLayout (view);  
        File.Delete (readFileSdcardFile(sdCardPath)); 



    }



    public String readFileSdcardFile(String path) {
        if (File.Exists (path)) {
            var str = File.ReadAllText (path);
            return str;
        } else {
            return "";
        }
    }   

Android LIstView适配器删除文件和查看项目点击

您正在传递sdcard路径而不是文件路径

替换中的OnItemClick

File.Delete (readFileSdcardFile(sdCardPath));

File.Delete (readFileSdcardFile(textPath)); 

File.Delete ("file path"); 

您是否在清单中将正确的权限设置为true ?

像这样:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

还有,为什么要通过传递内容作为参数来删除文件?您不返回路径,而是返回文件的内容。System.IO.Delete函数获取文件的路径并删除它。所以只要File.Delete(sdCardPath)应该足够,如果你的文件路径是正确的。