0

I have some code which should allow a user to delete a folder (which it does) then it should be removed from the ‘recent folders’ list - however it is not. The folder remains on the ‘recent folders’ list even after the user has deleted it - which is undesired.

/**
* Add recent folders to the list in order as acquired by the {@link RecentFolderList}.
*
* @param destination List of drawer items to populate
*/
private void addRecentsToList(List<DrawerItem> destination) {
    // If there are recent folders, add them.
    final List<Folder> recentFolderList = getRecentFolders(mRecentFolders);
    // Remove any excluded folder types
    if (mExcludedFolderTypes != null) {
        final Iterator<Folder> iterator = recentFolderList.iterator();
        while (iterator.hasNext()) {
            if (isFolderTypeExcluded(iterator.next())) {
                iterator.remove();
            }
        }
    }
    if (recentFolderList.size() > 0) {
        destination.add(DrawerItem.ofHeader(mActivity, R.string.recent_folders_heading,
        mBidiFormatter));
        // Recent folders are not queried for position.
        for (Folder f : recentFolderList) {
            destination.add(DrawerItem.ofFolder(mActivity, f, DrawerItem.FOLDER_RECENT,
            mBidiFormatter));
        }
    }
}

The strange part is if I perform an orientation change - the folders are removed from the 'recent folders list' as desired - and I’m unsure as to why this might be happening.

I have a feeling the list may simply need to be refreshed (after the folder has been deleted) however I am unsure.

Any suggestions, clues or pointers are greatly appreciated.

Full Source:

http://pastebin.com/fiX4S9fB

P.S.

Calling notifyDataSetChanged() on the adapter (as suggested by one of the answers below - and whomever flagged this as a duplicate - does not seem to have any effect)

  • possible duplicate of [How to refresh Android listview?](http://stackoverflow.com/questions/2250770/how-to-refresh-android-listview) – Grzegorz Żur Nov 21 '14 at 17:25
  • This is not a duplicate - we've tried notifyDatasetChanged and it isn't working... (see answers/comments below) there is something else going on here – Christopher Hammond Nov 21 '14 at 18:04
  • Mind removing that duplicate flag? The fix shown in the article is not working (notifyDataSetChanged does not resolve the issue) – Christopher Hammond Nov 21 '14 at 18:19

3 Answers3

0

Call invalidate method on ListView to cause the refresh or notifyDatasetChanged on the adapter.

Grzegorz Żur
  • 47,257
  • 14
  • 109
  • 105
  • mListView.invalidate(); does not refresh mRecentFolders (I need to refresh the recent folder list specifically) and invalidate is not an option for mRecentFolders – Christopher Hammond Nov 21 '14 at 17:31
  • I implemented it as follows - the recent folders list still contains the deleted folder after it has been deleted: http://pastebin.com/pCxeTv5X ...and changing orientation still removes it. – Christopher Hammond Nov 21 '14 at 17:47
  • See anything I did wrong? I'm still not sure why the recent folders list won't update (without changing orientation). Honestly - I would think notifyDatasetChanged would do it - unless perhaps there is an issue w my bundle or something else overriding it) if you see anything I should be aware of - please let me know. – Christopher Hammond Nov 21 '14 at 19:49
0

If you perform an orientation change your activity gets destroyed and rebuild. Then your member variable mRecentFolders might be empty. To prevent this add the last line in the following code in your AndroidManifest.xml to your activity:

<activity
android:name="name"
android:label="label"
android:configChanges="orientation|keyboardHidden|screenSize"> /*add this line*/
FreshD
  • 2,914
  • 2
  • 23
  • 34
  • I do not want to prevent the 'recent folders list' from refreshing on orientation change... that is fine - I'd like to refresh the recent folders list once the folder has been deleted (to then show the list with the deleted folder removed from it) how might that be accomplished? – Christopher Hammond Nov 21 '14 at 17:38
  • See anything else I might look into? – Christopher Hammond Nov 21 '14 at 19:49
0

I looked at your code and it seems like you have a lot of different copies of the list you're working with. My guess is that you're deleting the folder from one of the lists, but not from the others.

The way ListAdapters in Android work is: you create them with a reference to a data source (say a list or array) and then when you change that data source, you have to notify them using notifyDataSetChanged() and it's their responsibility to refresh the view. If this is not happening in your case, that means the data source with which you created your list adapter is not getting updated... and that's just a matter of your internal app logic and has nothing to do with Android.

Also, I noticed that your code has multiple adapters and some of them override notifyDataSetChanged() and don't call the super-implementation. Make sure you're not breaking the chain by doing this.

Oh, and the reason why it gets refreshed when you rotate the screen is that the entire Activity gets recreated at that point and all your data gets re-loaded from the ultimate source as if you're opening that screen for the first time (or from passed data from the old instance of the Activity, if you've set it up that way).

Todor K.
  • 653
  • 6
  • 12
  • Ok. I appreciate it... I thought it was bizarre that notifyDataSetChanged() wouldn't refresh the recentfolderslist. Do you see anything specifically in my logic that might prevent this from happening? I'm starting to think it may have to do with the data I'm putting and getting from the bundle - but I'm not sure at this point (this has been stumping me for days) – Christopher Hammond Nov 21 '14 at 18:51
  • Do you see anything in particular I should focus on... I never imagined that refreshing myRecentFolders list would be so tricky. – Christopher Hammond Nov 21 '14 at 19:37
  • or perhaps show how I might implement the super-implementation in order to refresh it properly? (I'll gladly test it and accept your answer within a few minutes if we can get it working) – Christopher Hammond Nov 21 '14 at 19:43
  • Hey, sorry, I don't have any specific recommendation because there's a lot of code there (in your link), so I couldn't follow what was actually changing when a user deletes a recent folder. But I think some basic debugging will probably allow you to find the issue. Just attach a debugger and inspect the array or list that you passed to the adapter and see if its content changes as you expect. Good luck :) – Todor K. Nov 21 '14 at 22:39