1

我有一个列表,其中显示了我指定的目录中的项目列表。我创建了一个 onLongClickListener,它会弹出一个对话框进行确认。当用户按下对话框上的确定按钮时,我需要删除 SD 卡上的实际文件。我查看了在 Stack Overflow 上可以找到的所有示例,但没有一个对我有用。

public class ReadFilesFromPath extends Activity {
/** Called when the activity is first created. */
List<String> myList;
File file;
ListView listview;
ArrayAdapter<String> adapter;
String value;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recordinglist);
Intent intent = getIntent();
value = intent.getStringExtra("path"); //if it's a string you stored.
listview = (ListView) findViewById(R.id.recordlist);
myList = new ArrayList<String>();
onitemclick();
File directory = Environment.getExternalStorageDirectory();
file = new File( directory + "/" + "Recordify" );
File list[] = file.listFiles();

for( int i=0; i< list.length; i++)
{
        myList.add( list[i].getName() );
}
adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, android.R.id.text1, myList);
listview.setAdapter(adapter); //Set all the file in the list.
longclick();
}

public void longclick() {
    listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View v, //when long pressed 
                int pos, long arg3) {
            AlertDialog.Builder adb=new AlertDialog.Builder(ReadFilesFromPath.this); //alert for each time an item is pressed
              adb.setTitle("Delete?");
              adb.setMessage("Are you sure you want to delete this recording?");
              final int positionToRemove = pos;
              adb.setNegativeButton("Cancel", null);
              adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {

                  public void onClick(DialogInterface dialog, int which) {
                      listview.animate().setDuration(500).alpha(0) //animates a smooth deletion animation
                        .withEndAction(new Runnable() {
                          @Override
                          public void run() {
                            file.delete();
                            myList.remove(positionToRemove); //removes the selected item from the list but not on SD card
//this is where I need my code to delete it on the SD card to go.
                            adapter.notifyDataSetChanged(); //tells the adapter to delete it
                            listview.setAlpha(1); 
                          }
                        }); 
                  }});
              adb.show();
              return false;
              }
          });

        } 

 }
4

2 回答 2

0
String file_name = file_list.get(position);
String MEDIA_PATH = new String(Environment.getExternalStorageDirectory() + "/AudioRecorder/" + file_name);
File file = new File(MEDIA_PATH);
file.delete();
file_list.remove(position);
notifyDataSetChanged();

在单击事件处理程序中使用它。

于 2015-03-11T10:49:17.150 回答
0

你有没有尝试过:

new File(path).delete()
于 2013-08-19T08:03:00.540 回答