我有聊天屏幕,我正在從FirebaeStorage下載附件。 我有各種格式的文件,可以發送doc,pdf,apk等,每個我有相同的TextViews和ImageViews。將onPostExecute值返回到RecyclerAdapter的onBindViewHolder中
在聊天屏幕的recyclerview適配器我設置本地存儲的文件路徑,它可以通過運行AsyncTask從Firebase存儲中下載文件並返回文件路徑來獲取。此工作完美,但問題是如何找回文件路徑onBindViewHolder上特別是如果其他
這裏是我的RecyclerAdapter這裏我打電話的AsyncTask和需要結果返回到相同的範圍和等到下載完成,然後根據數據集視圖返回
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");
}
}
的AsyncTask
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();
}
}
UPDATE作爲@Atef野兔建議,我沒有在code.its實施工作正常,但如果我有不同的格式。如果從Asynctask獲得結果後如何調用特定的cuz代碼,建議只在調用pdf時聲明。
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"
}
見我的答案[這裏](http://stackoverflow.com/a/42656227/5993410) –
@AtefHares我使用界面來獲得結果回到活動,但問題來了,我不得不等待下載完成,然後設置與數據的意見,並沒有等待它返回null – Ritu
爲什麼你從調用調用Asynctask [R?你不應該使用適配器,除非你有數據將被顯示,而且,不要從適配器調用asynctask,從活動/片段調用它並獲取所需的數據,然後將其傳遞給適配器以顯示它 –