问题
/*Files will be clicked here*/
@Override
protected void onListItemClick(ListView l,View v, final int position, long id){
filePosition=new File(path1.get(position));
System.out.println("File position:"+filePosition);
if(filePosition.isDirectory()){
if(filePosition.canRead())
getDirectories(path1.get(position));
else
{
new AlertDialog.Builder(VideoSDcard.this)
.setIcon(R.drawable.folder)
.setTitle("["+filePosition.getName()+"]folder can't be read!")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
}
else {
AlertDialog.Builder alert=new AlertDialog.Builder(this);
alert.setIcon(R.drawable.folder);
alert.setTitle("Do you want to Delete this File from SD card ?"+" [ "+filePosition.getName()+" ] ");
//.setTitle("["+filePosition.getName()+"]")
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
/*Deleting a File*/
deleteFile(); // here deleting a file
}
private void deleteFile() {
boolean deleted= filePosition.delete();
}
});
I am browsing the files and directories from SD card. So Iam displaying it by using listview. When I don't need a file on sd card so I could be able to delete a file.
My problem is, after I deleting a file it has deleted completely on SD card but it is visible on listview. I meant in emulator. After deleting a file from the list , it should be disappear.
For an example:- I am having song.mp4 file on sd card. Once I deleted this file "song.mp4" then it should not be displayed again in a listview.
How should I do this?
回答1:
add this line
adapter.notifyDataSetChanged();
after
deleteFile()
method in onclick event..
or
call same activity using intent after that..
Try like this:
adapter = new MyListAdapter(this);
lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(MyActivity.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete " + position);
final int positionToRemove = position;
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MyDataObject.remove(positionToRemove);
adapter.notifyDataSetChanged();
}});
adb.show();
}
});
回答2:
You can make a function to update your listdataitem, and call this function just after deleting a file from sdcard. It will referesh your listItem. You may call the same function for first time to setdata list.
ActivityClass.java
ListView listview;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(android.R.id.list);
refreshData();
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//show your dialogbox here
//and on click of dialog's delete button referesh listview adapter and set it in listview.
.
.
AlertDialog.Builder alert=new AlertDialog.Builder(this);
alert.setIcon(R.drawable.folder);
alert.setTitle("Do you want to Delete this File from SD card ?"+" [ "+filePosition.getName()+" ] ");
//.setTitle("["+filePosition.getName()+"]")
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
/*Deleting a File*/
deleteFile(); // here deleting a file
refreshData();//refersh listItem
}
private void deleteFile() {
boolean deleted= filePosition.delete();
}
}
});
}
public void refreshData(){
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
}
来源:https://stackoverflow.com/questions/16982647/how-can-i-make-a-file-to-disappear-once-if-it-is-deleted-in-android-sd-card