gpt4 book ai didi

java - 将 onPostExecute 值返回到 RecyclerAdapter 的 onBindViewHolder

转载 作者:行者123 更新时间:2023-11-30 00:36:39 25 4
gpt4 key购买 nike

我有聊天屏幕,我正在从 FirebaeStorage 下载附件。我有各种格式的文件,可以发送 doc、pdf、apk 等,对于每一种,我都有相同的 TextViews 和 ImageViews。

在聊天屏幕的 recyclerview 适配器中,我正在设置本地存储的文件路径,这可以通过运行 AsyncTask 获得,它从 Firebase 存储下载文件并返回文件路径。这非常有效,但问题是如何在 onBindViewHolder 中取回该文件路径特别是 if else

这是我的 RecyclerAdapter,我在其中调用 AsyncTask 并需要将结果返回到相同的范围,等待下载完成,然后根据返回的数据设置 View

public void onBindViewHolder(final Chat_Adapter.ViewHolder holder, int position) {


if (fileType.equals("pdf")){
new DownloadFileFromFS(Download_URL,FileName+".pdf").execute();

//HERE I NEED THE RESULT FROM ASYNCTASK AND WAIT TILL DOWNLOAD COMPLETES
//THEN SET THE VIEWS WITH RETURN RESULT FROM ASYNCTASK

if (DownloadFilePath!=null){
File file=new File(DownloadFilePath);
long sizeFile=file.length()/1024; //In KB
holder.Doc_FileName.setText(DownloadFilePath);
holder.Doc_FileSize.setText(String.valueOf(sizeFile));
}
else {
Log.d(TAG,"DOWNLOAD FILE PATH IS NULL");
}

}

异步任务

public class DownloadAttachment extends AsyncTask<Void, String, String> {
String DownloadUrl,fileName;
File file;
Context context;
ProgressBar progressBar;
public static final String TAG="###Download Attachment";

public DownloadAttachment(Context context, String downloadUrl, String fileName) {
DownloadUrl = downloadUrl;
this.fileName = fileName;
this.context=context;
}


@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}

@Override
protected String doInBackground(Void... params) {
int count;
try {
File root = android.os.Environment.getExternalStorageDirectory();

Log.d(TAG,"DO IN BACKGROUND RUNNING");
File dir = new File(root.getAbsolutePath() + "/Downloaded Files/");
if (dir.exists() == false) {
dir.mkdirs();
}


URL url = new URL(DownloadUrl); //you can write here any link
file = new File(dir, fileName);

long startTime = System.currentTimeMillis();
Log.d(TAG, "download begining");
Log.d(TAG, "download url:" + url);
Log.d(TAG, "downloaded file name:" + fileName);

/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();

//this will be useful so that you can show a typical 0-100% progress bar
int lengthOfFile=ucon.getContentLength();
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);

/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayOutputStream baf = new ByteArrayOutputStream(5000);
int current = 0;
long total=0;
while ((current = bis.read()) != -1) {
baf.write((byte) current);
total=total+current;
//PUBLISH THE PROGRESS
//AFTER THIS onProgressUpdate will be called
publishProgress(""+(int)(total*100)/lengthOfFile);
}


/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.flush();
fos.close();
Log.d("DownloadManager", "download ready in " + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
Log.d(TAG,"File Path "+file);

} catch (IOException e) {
Log.d("DownloadManager", "Error: " + e);
}

return file.toString();
}

}

更新 正如@Atef Hares 所建议的,我确实在代码中实现了它。它工作正常但如果我有不同的格式怎么办。从 asynctask 获得结果后如何调用特定的 if else 因为代码建议只调用 pdf if 语句。

if (fileType.equals("pdf")){
final String nameFile=UUID.randomUUID().toString();
new DownloadFileFromFS(chat_wrapper.getDocuments(),nameFile).execute();

filePathInterface=new FilePath() {
@Override
public void LocalFilePath(final String Path) {
//HERE I AM GETTING RESULT FROM ASYNCTASK
//AND SETTING VIEWS ACCORDING TO IT
}
};


}
else if (fileType.equals("doc")){
//HOW TO GET RESULT FROM ASYNCTASK HERE IF FILETYPE IS "doc"

}
else if (fileType.equals("ppt")){
//HOW TO GET RESULT FROM ASYNCTASK HERE IF FILETYPE IS "ppt"
}

最佳答案

好的,如您所知,使用接口(interface)将实现您的需求!

就这样吧:

public interface AsyncTaskCallback {
void onSuccess (String filePath);
}

并且在适配器中,不要向 View 设置任何数据,除非您从 asyncTask 中获取数据,如下所示:

public void onBindViewHolder(final Chat_Adapter.ViewHolder holder, int position) {

//Initialize filetype here

new DownloadFileFromFS(Download_URL, FileName + "."+ filetype, new AsyncTaskCallback() {
@Override
public void onSuccess(String filePath) {
//HERE I NEED THE RESULT FROM ASYNCTASK AND WAIT TILL DOWNLOAD COMPLETES
//THEN SET THE VIEWS WITH RETURN RESULT FROM ASYNCTASK

if (filePath != null) {
File file = new File(filePath);
long sizeFile = file.length() / 1024; //In KB
holder.Doc_FileName.setText(filePath);
holder.Doc_FileSize.setText(String.valueOf(sizeFile));
} else {
Log.d(TAG, "DOWNLOAD FILE PATH IS NULL");
}
}
}).execute();
}

关于java - 将 onPostExecute 值返回到 RecyclerAdapter 的 onBindViewHolder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43309214/

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