Search code examples
androiddropbox

sort file and folders in dropbox


I am implementing dropbox core api in my app.I want to show the folders first and then files when the dropbox files bind in listview.

I am using following code:

private ArrayList<Entry> files,onlyfiles;
private ArrayList<String> dir;
public void setLoggedIn(final boolean loggedIn) {
        pd = ProgressDialog.show(DropboxDownload.this, null,
                "Retrieving data...");
        new Thread(new Runnable() {

            @Override
            public void run() {
                Constants.mLoggedIn = loggedIn;
                if (loggedIn) {
                    int i = 0;
                    com.dropbox.client2.DropboxAPI.Entry dirent;

                    try {
                        dirent = mApi.metadata(DIR, 1000, null, true, null);
                        files = new ArrayList<com.dropbox.client2.DropboxAPI.Entry>();
                        onlyfiles = new ArrayList<com.dropbox.client2.DropboxAPI.Entry>();
                        dir = new ArrayList<String>();
                        for (com.dropbox.client2.DropboxAPI.Entry ent : dirent.contents) {
                            if(ent.isDir){
                            files.add(ent);
                            }
                            else{
                            onlyfiles.add(ent);
                            }
                            files.addAll(onlyfiles);
                            dir.add(new String(files.get(i++).path));
                        }
                        i = 0;
                        pd.dismiss();
                        mHandler.sendEmptyMessage(0);

                    } catch (DropboxException e) {
                        e.printStackTrace();
                        pd.dismiss();
                    }
                }
                pd.dismiss();
            }
        }).start();

    }

But the above code causes a file to repeat several times in the list.Ehat is the problem??


Solution

  • entries = new ArrayList<com.dropbox.client2.DropboxAPI.Entry>();
    
    dirs = new ArrayList<String>(); // list of all directories
    files = new ArrayList<String>(); // list of all files
    
    // enumerate all entries (both files and directories)
    for (com.dropbox.client2.DropboxAPI.Entry ent : dirent.contents) {
        // put directories into dirs
        if(ent.isDir) { dirs.add(ent); }
    
        // put files into files
        else { files.add(ent); }
    }
    
    // add all the directories first
    entries.addAll(dirs);
    
    // then add all the files
    entries.addAll(files);