Help refreshing list with notifyDataSetChanged()

人走茶凉 提交于 2019-12-24 00:56:53

问题


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();
  }
 }

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/5330536/help-refreshing-list-with-notifydatasetchanged

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!