我正在尝试在我的应用中显示Dropbox帐户中的文件列表。我的问题是,如何将列表(fnames)返回到我的MainActivity中,以便我可以在列表视图中显示它。 我如何在MainActivity中调用它? Android开发新手,任何其他提示将不胜感激!
谢谢!
public class DbFileExplorer extends AsyncTask<Void, Long, Boolean> {
private Context mContext;
private final ProgressDialog mDialog;
private DropboxAPI<?> mApi;
private String mPath;
private FileOutputStream mFos;
private boolean mCanceled;
private Long mFileLen;
private String mErrorMsg;
protected String[] fnames;
public DbFileExplorer(Context context, DropboxAPI<?> api,
String dropboxPath, String[] efnames){
// We set the context this way so we don't accidentally leak activities
mContext = context.getApplicationContext();
fnames = efnames;
mApi = api;
mPath = dropboxPath;
mDialog = new ProgressDialog(context);
mDialog.setMessage("Opening Directory");
mDialog.show();
}
@Override
protected Boolean doInBackground(Void... params){
// Get the metadata for a directory
int i = 0;
try{
fnames = null;
Entry dirent = mApi.metadata(mPath, 1000, null, true, null);
ArrayList<Entry> files = new ArrayList<Entry>();
ArrayList<String> dir = new ArrayList<String>();
for (Entry ent: dirent.contents){
//Add it to the list of thumbs we can choose from
files.add(ent);
dir.add(new String(files.get(i++).path));
}
i=0;
fnames = dir.toArray(new String[dir.size()]);
return true;
} catch (DropboxUnlinkedException e) {
// The AuthSession wasn't properly authenticated or user unlinked.
} catch (DropboxPartialFileException e) {
// We canceled the operation
mErrorMsg = "Download canceled";
} catch (DropboxServerException e) {
// Server-side exception. These are examples of what could happen,
// but we don't do anything special with them here.
if (e.error == DropboxServerException._304_NOT_MODIFIED) {
// won't happen since we don't pass in revision with metadata
} else if (e.error == DropboxServerException._401_UNAUTHORIZED) {
// Unauthorized, so we should unlink them. You may want to
// automatically log the user out in this case.
} else if (e.error == DropboxServerException._403_FORBIDDEN) {
// Not allowed to access this
} else if (e.error == DropboxServerException._404_NOT_FOUND) {
// path not found (or if it was the thumbnail, can't be
// thumbnailed)
} else if (e.error == DropboxServerException._406_NOT_ACCEPTABLE) {
// too many entries to return
} else if (e.error == DropboxServerException._415_UNSUPPORTED_MEDIA) {
// can't be thumbnailed
} else if (e.error == DropboxServerException._507_INSUFFICIENT_STORAGE) {
// user is over quota
} else {
// Something else
}
// This gets the Dropbox error, translated into the user's language
mErrorMsg = e.body.userError;
if (mErrorMsg == null) {
mErrorMsg = e.body.error;
}
} catch (DropboxIOException e) {
// Happens all the time, probably want to retry automatically.
mErrorMsg = "Network error. Try again.";
} catch (DropboxParseException e) {
// Probably due to Dropbox server restarting, should retry
mErrorMsg = "Dropbox error. Try again.";
} catch (DropboxException e) {
// Unknown error
mErrorMsg = "Unknown error. Try again.";
}
return false;
}
@Override
protected void onProgressUpdate(Long... progress){
int percent = (int)(100.0*(double)progress[0]/mFileLen + 0.5);
mDialog.setProgress(percent);
}
@Override
protected void onPostExecute(Boolean result){
mDialog.dismiss();
}
private void showToast(String msg){
Toast error = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
error.show();
}
}
答案 0 :(得分:3)
AsyncTask
模板的第三个参数是任务的结果类型。将其从Boolean更改为ArrayList或类似的结构。然后从doInBackground
方法返回该值。然后它将被传递到onPostExecute
的{{1}}方法,该方法将在UI线程上执行(因此,如果需要,您可以使用数据来操作UI)。
答案 1 :(得分:0)
我使用execute()。get();
来解决这类问题实施例
从活动或片段调用
ArrayList<String> arraylistitem= new jsonparsing(getActivity()).execute().get();
In async task
public class jsonparsing extends AsyncTask<Void, Void, ArrayList<String>>
{
public jsonparsing(Activity activity)
{
this.activity = activity;
arraylistitem = new ArrayList<String>();
}
protected ArrayList<String> doInBackground(Void... arg0)
{
// Do anything else you need to with the returned list here
return arraylistitem;
}
}