我正在我的app中实现dropbox core api。我想首先显示文件夹,然后在dropview文件在listview中绑定时显示文件。
我正在使用以下代码:
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();
}
但是上面的代码导致文件在列表中重复多次。问题是什么?
答案 0 :(得分:0)
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);