gpt4 book ai didi

android - 如何将数组列表从 AsyncTask 返回到 Main Activity?

转载 作者:行者123 更新时间:2023-11-29 17:52:40 26 4
gpt4 key购买 nike

我正在尝试在我的应用中显示保管箱帐户中的文件列表。我的问题是,如何将列表 (fnames) 返回到我的 MainActivity 中,以便我可以在 ListView 中显示它。以及如何在 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();
}
}

最佳答案

AsyncTask 模板的第三个参数是任务的结果类型。将其从 Boolean 更改为 ArrayList 或类似结构。然后从您的 doInBackground 方法返回该值。然后它将传递给 AsyncTaskonPostExecute 方法,该方法将在 UI 线程上执行(因此您可以根据需要使用数据来操作 UI)。

关于android - 如何将数组列表从 AsyncTask 返回到 Main Activity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21894255/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com