为什么我的文件成倍增加???? renameTo();

时间:2016-10-16 06:29:50

标签: java android file-rename

我有一个重命名文件的问题,每次我尝试它们最终都会成倍增加并且创建的数量是I.e.的两倍。如果我想在列表视图中重命名一首歌并按住长按事件,则会在我的列表视图中将数量增加一倍,比如2到4首歌曲和4到12首等等。我正在使用OnItemLongClickListener处理为每个项目创建的文件,单击要重命名的列表视图。我不知道为什么它不起作用......我甚至试过把位置设置为

    String pos = lv.getItemAtPosition(position); 

还有很多方法...... 但没有用它最终成为一个随机数增加...我的问题是为什么;如何解决。

    downloadsList.setOnItemLongClickListener(
        new OnItemLongClickListener(){
            @Override
            public boolean onItemLongClick(AdapterView<?> parent , View view , int position , long itemId ){
                EditText bar = (EditText)findViewById(R.id.search);
                String to = bar.getText().toString();
(ListView) findViewById(R.id.downloads);
                File[] pos = files.listFiles();
                File filein = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() , files.getName());
                File fileTo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/" + to);
                if(!filein.exists()){
                    filein.renameTo(fileTo);
                } else {
                    try {
                        throw new IOException("File exists");
                    }catch (IOException e){
                           e.printStackTrace();
                    }
                }

                finish();
                overridePendingTransition(0, 0);
                startActivity(getIntent());
                overridePendingTransition(0, 0);
                return true;
            }
        });

以下是填充它的列表视图方法

    private void ShowLists(){
    files = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_DOWNLOADS).toString());

    downloads = files.listFiles(new FileFilter(){
        @Override
        public boolean accept(File file){
            String ext = file.getName();
            if(ext.endsWith(".mp3")){
                return true;
            }
            return false;
        }
    });
    mp3s = new String[downloads.length];
        for(int i = 0 ; i < mp3s.length; i++){
            mp3s[i] = downloads[i].getName();       
        }
    adapter = new ArrayAdapter<String>(this,R.layout.downloads, R.id.textviewone , mp3s);
    ListView downloadsList = (ListView) findViewById(R.id.downloads);
    downloadsList.setAdapter(adapter);
}

这是在http://imgur.com/rBmSAXK

之前

这是在我点击长按以重命名文件和正在进行的复杂化http://imgur.com/I9szC6m

之后

更新代码

    public boolean onItemLongClick(AdapterView<?> parent , View view , int position , long itemId ){
                EditText bar = (EditText)findViewById(R.id.search);
                String to = bar.getText().toString();
                ListView lv = (ListView) findViewById(R.id.downloads);
                String pos = lv.getItemAtPosition(position).toString();
                File filein = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() , pos);
                File fileTo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() , to);
                filein.renameTo(fileTo);
                finish();
                overridePendingTransition(0, 0);
                startActivity(getIntent());
                overridePendingTransition(0, 0);
                return true;
            }
        });

0 个答案:

没有答案