安卓multichoicemodelistener删除内部存储文件 [英] android multichoicemodelistener delete internal storage file

查看:151
本文介绍了安卓multichoicemodelistener删除内部存储文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我有在启用multichoicemodelistener一个ListView。我想删除内部存储文件,使用multichoicemodelistener(即在我的列表视图显示文件)。但没有运气。

In my app i have a Listview where multichoicemodelistener is enabled. I want to delete the internal storage files (files that is shown in my listview) using multichoicemodelistener. But with no luck.

下面是我的code

public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {



            case R.id.list_context_delte:
                SparseBooleanArray sparseBooleanArray = getListView().getCheckedItemPositions();
                for(int i = sparseBooleanArray.size() -1; i >= 0; i--)
                    context.deleteFile(sparseBooleanArray.keyAt(i));

                mAdapter.notifyDataSetChanged();
                mode.finish();

                Toast.makeText(ShowListActivity.this, R.string.deleted, Toast.LENGTH_SHORT).show();
                mode.finish();
        }
        return false;

    }

我得到了一个错误,指出:在类型上下文的方法DELETEFILE(字符串)不适用于参数(INT)

I got an Error that says: The method deleteFile(String) in the type Context is not applicable for the arguments (int)

任何想法?

更新
我已经编辑我的code,所以知道它看起来像

UPDATE I have edited my code, so know it look like

 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {         
       case R.id.list_context_delte:
                nr = 0;
                SparseBooleanArray sparseBooleanArray = getListView().getCheckedItemPositions();

                for(int i = sparseBooleanArray.size() -1; i >= 0; i--)
                    if (sparseBooleanArray.get(i)) {
                        String items =  getListView().getAdapter().getItem(sparseBooleanArray.keyAt(i)).toString(); 

                        File dir = getFilesDir();
                    File file = new File(dir, (items));
                    file.delete();
                    RowItem selecteditem = mAdapter.getItem(sparseBooleanArray.keyAt(i));
                    mAdapter.remove(selecteditem);
                     mAdapter.notifyDataSetChanged();
                    Toast.makeText(ShowListActivity.this,items+ R.string.deleted, Toast.LENGTH_SHORT).show();


                       }
                    mode.finish();


        }



        return false;

    }

在我preSS删除按钮,该文件(S)已经一去不复返了。但是,当我走出去的活动,并返回到该活动的所有被删除的文件又回来了。
是不是文件从内部存储正确删除?
是否有人有什么建议?

After I press the delete button, the file(s) is gone. But when I go out of the activity and go back to the activity all the deleted files is back. Are the files not deleted correctly from the internal storage ? Does someone have a suggestions?

推荐答案

假设该文件存在,看起来像你的code将删除file.you可以添加一行的安全。

Assuming that the file exists, looks like your code will delete the file.you can add a line for safety.

if(file.exists()){
  boolean isDeleted = file.delete();
  Log.v(TAG,"file delection is success : "+isDeleted);
}

我觉得实际的问题存在该文件确实会被删除here.Its可能的,但你不会很快刷新listview.As为您删除的文件,在适配器中删除的项目,并更新列表view.Like这样的:

I think the actual problem exists here.Its possible that file is actually deleted but you are not refreshing the listview.As soon as you delete the file,delete the item in the adapter and update the list view.Like this:

     for(int i = sparseBooleanArray.size() -1; i >= 0; i--)
            if (sparseBooleanArray.get(i)) {
                String items =  getListView().getAdapter().getItem(sparseBooleanArray.keyAt(i)).toString();
        File dir = getFilesDir();
        File file = new File(dir, (items));
        boolean isDeleted = file.delete();
          if(isDeleted){
              mAdapter.deleteItem(items); // create a  method in adapter which will delete the item.
              mAdapter.notifyDataSetChanged();

        Toast.makeText(ShowListActivity.this,items+ R.string.deleted, Toast.LENGTH_SHORT).show();
            }

            }

            mode.finish();

这篇关于安卓multichoicemodelistener删除内部存储文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆