0

I currently develop a File Explorer app and I need to have a auto refresh for my listview. Because when I delete the item I still can see the item , I need to back the folder and go inside the folder again only see it disappear. How to auto refresh so when I deleted the item it disappear itself.

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    super.onListItemClick(l, v, position, id);
    FileInfo fileDescriptor = fileArrayListAdapter.getItem(position);
    if (fileDescriptor.isFolder() || fileDescriptor.isParent()) {
        currentFolder = new File(fileDescriptor.getPath());
        fill(currentFolder);
        //


    } else {

        fileSelected = new File(fileDescriptor.getPath());
        Intent intent = new Intent();
        intent.putExtra(Constants.KEY_FILE_SELECTED,
                fileSelected.getAbsolutePath());
        setResult(Activity.RESULT_OK, intent);
        Log.i("FILE CHOOSER", "result ok");



            MimeTypeMap map = MimeTypeMap.getSingleton();
            String ext = MimeTypeMap.getFileExtensionFromUrl(fileSelected.getName());
            String type = map.getMimeTypeFromExtension(ext);

            if (type == null){
                type = "*/.jpeg*";

            intent.setAction(android.content.Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(fileSelected), "image/*");

          //  intent.setDataAndType(data, type);
            }else {
                 type = "*/.txt*";

                    intent.setAction(android.content.Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(fileSelected), "text/*");

                 //   intent.setDataAndType(data, type);
            }
        //finish();


    //  intent.setAction(android.content.Intent.ACTION_VIEW);

        startActivity(intent); 
    //  Intent intent = new Intent();




        this.getListView().setLongClickable(true);
        this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
        new AlertDialog.Builder(ViewNoteActivity.this) .setTitle("Delete File")
        .setMessage("Do You Want To Delete this file ?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int button){
                fileSelected.delete();
                Toast.makeText(getBaseContext(), "File has been deleted." , Toast.LENGTH_SHORT ).show();
                adapter.notifyDataSetChanged();
            }

        })
        .setNegativeButton("No", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int button){
            }
        }).show();
         return true;
            }
        });

    }
}



public static boolean deleteDir(File dir) {
    if (dir.isDirectory()) {
        String[] children = dir.list();
        for (int i=0; i<children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }
    return dir.delete();

}
Frozen
  • 97
  • 1
  • 4
  • 15

2 Answers2

1

Delete item from the datasource that populates listview and call notifyDataSetChanged on the adapter to refresh listView

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

You need to delete ListItem from ArrayList from Adapter and call notifyDatasetChanged() , When you call notifyDatasetChanged() method it will refresh your listview with ArrayList with updated data so, you have to first delete item from ArrayList onClick of Delete Button. And then you have to call notifyDatasetChanged().

chirag patel
  • 148
  • 1
  • 9