I have a ListView populated with files as items. When I click on an item, an AsyncTask is launched, downloading the file. Now while a file is being downloaded, I have a ProgressBar spinning next to its name in the ListView.
My problem is : when I click on certain items, the spinning wheel is also visible on other items in the listview even though I didn't click on them.
CategoryActivity.java (Activity of the ListView, only the useful part) :
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Récupère les valeurs depuis ListItem
udl = ((TextView) view.findViewById(R.id.udl)).getText().toString();
loader = ((ProgressBar) view.findViewById(R.id.spinWheel2));
filepath = dirpath + udl + ".pdf";
File file = new File(filepath);
if (file.exists()) {
// If files exists I open it
}else{ // I download it
// There I set the ProgressBar to VISIBLE
loader.setVisibility(View.VISIBLE);
// Call the AsyncTask
final DownloadTask downloadTask = new DownloadTask(CategoryActivity.this);
downloadTask.execute();
}
}
});
}
public static String getUdl(){
return udl;
}
public static ProgressBar getLoader(){
return loader;
}
DownloadTask.java (only the useful part) :
public class DownloadTask extends AsyncTask<Void, Integer, String> {
public static String url1 = some url;
private static String url2 = "";
private static String url3 = CategoryActivity.getUdl();
public static ProgressBar loader = CategoryActivity.getLoader();
private static String url = "";
final static String dirpath = Environment.getExternalStorageDirectory().toString()+ "/SomeDirectory/";
public static String filepath = "";
public static final String PREFS_TEXT = "PrefsTextView";
private Context context;
public DownloadTask(Context context) {
this.context = context;
url3 = CategoryActivity.getUdl();
loader = CategoryActivity.getLoader();
}
@Override
protected String doInBackground(Void... params) {
SharedPreferences codeSaveUrl = context.getSharedPreferences(PREFS_TEXT,Context.MODE_PRIVATE);
url2 = codeSaveUrl.getString("defaut", ""); // Code Organisation
url = url1 + url2 + "&file=" + url3 ;
filepath = dirpath + url3 + ".pdf";
//Download of the file
HttpHandler hh = new HttpHandler();
try {
hh.download(url, filepath, this);
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
return null;
}
public void doProgress(int value){ // Méthode appelée dans HttpHandler
publishProgress(value);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
StorageDirectory sd = new StorageDirectory(); // Appel à la classe StorageDirectory
sd.callStorageDirectory(); // Création du répertoire de stockage sur mémoire externe
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
}
@Override // Evènement de post-execution
protected void onPostExecute(String result) {
// There I set the ProgressBar to GONE
loader.setVisibility(View.GONE);
if (result != null){
Toast.makeText(context,"Erreur lors du téléchargement : "+result, Toast.LENGTH_LONG).show();
}else{
// Note that this message is correctly shown on screen when the download finishes
Toast.makeText(context,"Le fichier a bien été téléchargé", Toast.LENGTH_SHORT).show();
}
}
}
Every file is downloaded when I click on them, the only problem is the spinning wheel being cloned on other items.
Edit : here are the images :
When I click on this item :
The spinning wheel of this item (which is not on the screen at the same time as the first) shows too (its file is not downloaded though) :
Thanks for your help.
Do ProgressBar
visible and invisible or gone in Asynctask
not in Activity
. If you use any Interface for callBack
to activity from asyctask then do it in activity.
call Asynctask
like this
// Don't set ProgressBar to VISIBLE here
//loader.setVisibility(View.VISIBLE);
// Call the AsyncTask
final DownloadTask downloadTask = new DownloadTask(CategoryActivity.this, loader);
downloadTask.execute();
Change DownloadTask's
constructer like this
public DownloadTask(Context context, ProgressBar pb) {
this.context = context;
this.pb = pb;//also declare pb globaly
url3 = CategoryActivity.getUdl();
}
in onPreExecute()
visible the ProgressBar
@Override
protected void onPreExecute() {
super.onPreExecute();
pb.setVisibility(View.VISIBLE);
StorageDirectory sd = new StorageDirectory();// Appel à la classe StorageDirectory
sd.callStorageDirectory();// Création du répertoire de stockage sur mémoire externe
}
in onPostExecute(String result)
change the visibility of ProgressBar
like this
pb.setVisibility(View.GONE);//instead of loader.setVisibility(View.GONE);
EDIT:
Try to use ViewHolder Pattern for adapter also you can take a look at this also
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.