我有一个列表,显示我指定的目录中的项目列表。我创建了一个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;
}
});
}
}
答案 0 :(得分:0)
你试过了吗?
new File(path).delete()
答案 1 :(得分: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();
在点击事件处理程序中使用此功能。