将切换方法调整为listView,删除手机内存中的文件

时间:2014-03-11 12:42:06

标签: java android listview

我有一个列表视图,显示存储在手机内存中的文件列表(没有文件扩展名)。 然后我实现了允许用户删除文件的OnItemLongClickListener

OnItemLongClickListener使用“switch”语句。

因为每次删除文件时列表视图中项目的位置都会改变,我想实现一个规则来检查文件的名称是否与相关文件匹配,无论文件的名称是移动到案例0,案例1等等。

要明确:

初始列表视图:

    switch          Listview items         related file to delete
    case 0;               A                          A.map
    case 1;               B                          B.map
    case 2;               C                          C.map
    case 3;               D                          D.map

删除B后的Listview:

    switch          Listview items         related file to delete
    case 0;               A                          A.map
    case 1;               C                          C.map
    case 2;               D                          

现在,如果我在案例1中编写了一条删除B.map的规则,那么它可以正常工作。 但现在项目C具有与案例1相同的规则,并且规则不匹配。

因为我是Android的新手,我不知道如何解决这个问题。

有人能帮助我吗?

这是活动

    public class MainActivity extends Activity {

ListView lv;
ArrayAdapter<String> adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lv = (ListView) findViewById(R.id.listView1);

    File dir = new File(Environment.getExternalStorageDirectory().getPath() + "/osmdroid/tiles/");
    File[] filelist = dir.listFiles();
    String[] theNamesOfFiles = new String[filelist.length];
    for (int i = 0; i < theNamesOfFiles.length; i++) {

        String temp = filelist[i].getName();
       theNamesOfFiles[i] = temp.substring(0, temp.lastIndexOf('.'));

    }
    Arrays.sort(theNamesOfFiles);
    adapter = new ArrayAdapter<String>(this, R.layout.list_row, theNamesOfFiles);

    lv.setAdapter(adapter);

    lv.setOnItemLongClickListener(new OnItemLongClickListener() {
        // setting onItemLongClickListener and passing the position to the function
                  @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {

                      switch(position){

                      case 0:{

                          }

                      break;

                      case 1:{

                          }
                          break;

                      case 2:{    


                          }
                          break;

                      case 3:{



                          }
                          break;

                      case 4:{

                          }

                          break;
                      }
            return true;
        }
    });
}



public void doDeleteFile(int position){

    File fileToDelete = new File(Environment.getExternalStorageDirectory().getPath() + "/osmdroid/tiles/", "A.map");

    if(!fileToDelete.isDirectory()){

        try{
            if(fileToDelete.delete()){  

                System.out.println("File delete operation success");
            }

            else{
                System.out.println("File delete operation failed");
            }
        }catch(Exception ex){
            System.out.println("Exception :"+ex.getMessage());
        }

    }else{

        System.out.println("It is  not a file");

    }

}

}

2 个答案:

答案 0 :(得分:2)

删除项目时也将其从数组中删除,然后在更新数组项后调用适配器上的notifyDataSetChange()

无需检查姓名。您可以按位置

进行检查

答案 1 :(得分:1)

我会这样做:

首先,我将了解有关这些项目的数据结构。

public class MyFile {
    File file;
    String fileName;

    public MyFile(File file)
    {
        this.file = file;
        this.fileName = file.getName();
        fileName = fileName.substring(0, fileName.lastIndexOf('.'));
    }

    @Override
    public String toString() {
        return this.fileName;
    }
}

toString()方法很重要。该方法在ListView中的ArrayAdapter中提供了文字。

然后我会像这样初始化适配器。

adapter = new ArrayAdapter<MyFile>(this, R.layout.list_row, arrayOfMyFileObjects);

最后在onItemLongClick()函数中,您可以像这样获取相应的MyFile对象

MyFile item = adapter.getItem(position);

然后您可以使用deleteFile(item.file);

删除

我认为以下代码应该这样做。

public class MainActivity extends Activity {

ListView lv;
ArrayAdapter<MyFile> adapter;

public void list() {

    lv = (ListView) findViewById(R.id.listView1);

    File dir = new File(Environment.getExternalStorageDirectory().getPath()
            + "/osmdroid/tiles/");
    File[] filelist = dir.listFiles();
    ArrayList<MyFile> theNamesOfFiles = new ArrayList<MyFile>();
    for (File temp : filelist) {
        theNamesOfFiles.add(new MyFile(temp));
    }
    Collections.sort(theNamesOfFiles, new Comparator<MyFile>() {

        @Override
        public int compare(MyFile lhs, MyFile rhs) {
            return lhs.toString().compareToIgnoreCase(rhs.toString());
        }
    });
    adapter = new ArrayAdapter<MyFile>(this, R.layout.list_row,
            theNamesOfFiles);

    lv.setAdapter(adapter);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_base);

    list();

    lv.setOnItemLongClickListener(new OnItemLongClickListener() {
        // setting onItemLongClickListener and passing the position to the
        // function
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {

            doDeleteFile(adapter.getItem(position).file);

            return true;
        }
    });
}

public void doDeleteFile(File fileToDelete) {
    if (!fileToDelete.isDirectory()) {

        try {
            if (fileToDelete.delete()) {

                System.out.println("File delete operation success");
            }

            else {
                System.out.println("File delete operation failed");
            }
        } catch (Exception ex) {
            System.out.println("Exception :" + ex.getMessage());
        }

    } else {

        System.out.println("It is  not a file");

    }

}

public class MyFile {
    File file;
    String fileName;

    public MyFile(File file)
    {
        this.file = file;
        this.fileName = file.getName();
        fileName = fileName.substring(0, fileName.lastIndexOf('.'));
    }

    @Override
    public String toString() {
        return this.fileName;
    }
}

}