2

Hi i'm working on a simple file browser that is a part of my app and I have added some code to delete files however I need the list of files to refresh after one item has been deleted. This is the code I have so far and I added a comment in where I call on fileList.notifyDataSetChanged(); however it doesn't work for me so basically what am i doing wrong here? Thank you for any help

private void getDir(String dirPath)
{

 item = new ArrayList<String>();
 path = new ArrayList<String>();

 File f = new File(dirPath);
 File[] files = f.listFiles();

 if(!dirPath.equals(root))
 {

  item.add(root);
  path.add(root);

  item.add("../");
  path.add(f.getParent());

 }

 for(int i=0; i < files.length; i++)
 {
   File file = files[i];
   path.add(file.getPath());
   if(file.isDirectory())
    item.add(file.getName() + "/");
   else
    item.add(file.getName());
 }

 ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.row, item);
 setListAdapter(fileList); 
}

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

  final File file = new File(path.get(position));

  if (file.isDirectory())
  {
   if(file.canRead())
    getDir(path.get(position));
   else
   {
    new AlertDialog.Builder(this)
    .setIcon(R.drawable.boot)
    .setTitle("[" + file.getName() + "] folder can't be read!")
    .setPositiveButton("OK", 
      new DialogInterface.OnClickListener() {

       @Override
       public void onClick(DialogInterface dialog, int which) {
       }
      }).show();
   }
  }
  else
  {
        final CharSequence[] items = {"Info", "Rename", "Delete"};

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Options for " + file.getName());
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                if (item == 0)
                {
                    AlertDialog.Builder builder = new AlertDialog.Builder(Installed.this);
                    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
                    builder.setIcon(R.drawable.info)
                           .setTitle(file.getName() + " info")
                           .setMessage("Path: " + file.getPath() + "\n\nSize: " + (double)file.length()/1024 + " mbs" + "\n\nModified: " + sdf.format(file.lastModified()))
                           .setCancelable(false)
                           .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                               public void onClick(DialogInterface dialog, int id) {
                                 dialog.cancel();
                               }
                           }).show();

                }
                else if (item == 1)
                {
                    //TODO Add Rename code here
                    Toast.makeText(Installed.this, "Rename", Toast.LENGTH_SHORT).show();
                }
                else if (item == 2)
                {
                    AlertDialog.Builder builder = new AlertDialog.Builder(Installed.this);
                    builder.setMessage("Are you sure you want to delete " + file.getName() +"?")
                           .setCancelable(false)
                           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                               public void onClick(DialogInterface dialog, int id) { 
                                   File f1 = new File(file.getPath());
                                    boolean success = f1.delete();
                                    if (!success){
                                    Toast.makeText(Installed.this, "Could not delete " + file.getName(), Toast.LENGTH_SHORT).show();  
                                    }else{
                                    Toast.makeText(Installed.this, file.getName() + " deleted!", Toast.LENGTH_SHORT).show();
                                    }
                                    //This is where I try to refresh the list
                                    fileList.notifyDataSetChanged();
                            }
                           })
                           .setNegativeButton("No", new DialogInterface.OnClickListener() {
                               public void onClick(DialogInterface dialog, int id) {
                                    dialog.cancel();
                               }
                           }).show();
                }
            }
        }).show();
  }
 }
GFlam
  • 1,109
  • 4
  • 25
  • 38
  • 2
    Check this out: http://stackoverflow.com/questions/4088862/android-list-view-refresh/4088889#4088889 read the comments – blindstuff Mar 16 '11 at 23:43
  • can any one help http://stackoverflow.com/questions/28148618/listview-not-refreshing-after-click-on-button –  Jan 28 '15 at 04:34

2 Answers2

2

After you delete the item, try using fileList.remove(theItemYouDeleted). That should remove the item from the Adapter. In other words you have to delete it from your data and from your adapter. I hope this helps

raukodraug
  • 11,519
  • 4
  • 35
  • 37
  • I added this `fileList.remove(file.getName());` to where i had this in my original post `//This is where I try to refresh the list fileList.notifyDataSetChanged();` However i get an error in eclipse saying fileList can not be resolved because there is no local variable i guess for fileList because it's outside of my onClicks? but if i move it in that causes more problems so i'm not understanding what i should do – GFlam Mar 16 '11 at 20:46
  • 1
    yeah, thats because `fileList` is not a global variable, define it as global, or you can get it with `getListAdapter()` – raukodraug Mar 16 '11 at 20:59
  • Alright well i added this then to where i had my original comment `getListAdapter(fileList); fileList.remove(file.getName());` and i still get the error about it not being resolved really sorry about not getting this i probably sound stupid but i am just learning like i said less than 2 weeks with this stuff now – GFlam Mar 16 '11 at 21:51
  • no man, no one should appologize for learning. You would have to do something like this `(ArrayAdapter)getListAdapter(fileList).remove(file.getName())` or something like this `ArrayAdapter adapter = (ArrayAdapter)getListAdapter(fileList);` `adapter.remove(file.getName());` – raukodraug Mar 16 '11 at 22:10
  • sorry i just read what i wrote, it should be `getListAdapter()` instead of `getListAdapter(fileList)` in all cases – raukodraug Mar 16 '11 at 22:19
  • That worked great however one small problem say i have a list that is like item 1, item 2, item 3 i then delete item 2 so my list is now item 1, item 3 and item 3 is now in the location of item 2 (before it was deleted) if i go to delete item 3 it returns the toast item 2 can not be deleted! that being because it was already deleted item 3 just took it's place in the list. What is the reason for this? also i added the code you gave me (the second one after or something like this) and i have 3 warnings those being (out of room next comment) – GFlam Mar 16 '11 at 22:34
  • two of the warnings are `ArrayAdapter is a raw type. References to generic type ArrayAdapter should be parameterized` and the third one is `Type safety: The method remove(Object) belongs to the raw type ArrayAdapter. References to generic type ArrayAdapter should be parameterized` thank you so much by the way for the help – GFlam Mar 16 '11 at 22:35
  • Alright yea i already parameterized and for sure i'll be playing with it a lot and will comment back here tomorrow if i don't get it tonight thanks a ton for all of the help – GFlam Mar 16 '11 at 23:46
  • I've been playing with it now for quite some time with no luck I can't get it to remove the data from the actual array it only removes it from the listview i've tried things like `adapter.remove(path.get(position));` or `adapter.notifyDataSetChanged();` but still nothing I have no idea what it's missing it removes it from the visual list but not the actual array list will check back for help and will continue to play with it thank you to all that have offered help – GFlam Mar 17 '11 at 14:05
  • oh yeah, you have to delete it from the array too, so do it as you were doing it before you posted your problem here, and also the `adapter.remove(object)` – raukodraug Mar 17 '11 at 14:11
  • Yea i already have adapter.remove(file.getName()); here is my current code pastebined so it's a bit easier to read http://pastebin.com/1VMCx1NE thanks again for checking back every once in a while i probably sounds stupid not getting this ha :) – GFlam Mar 17 '11 at 14:30
  • alright, so so should do something like this: `File file = (File)getListView().getItemAtPosition(position); file.delete(); adapter.remove(file.getName());` And thats all what you need to remove it ;) – raukodraug Mar 17 '11 at 15:44
  • That causes a force close every time i tired putting it in multiple ways as well lol do you have a gtalk ha – GFlam Mar 17 '11 at 16:08
  • lol, sure, whats ur email? what is the exception it is causing? – raukodraug Mar 17 '11 at 17:04
1

For that to work correctly you will need to modify the List that you give the Adapter and remove the deleted file before the call to notifyDataSetChanged().

Currently you aren't removing the data from the Adapter's list.

Update:

When you create the adapter with this code.

ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.row, item);

event though you deleted the file, the list (item in the code), still contains the data about the file. You will need to remove that data from the list to see the updates.

Robby Pond
  • 73,164
  • 16
  • 126
  • 119