Android 异步任务文件下载时listview中的进度条
我遇到了一个奇怪的问题,每当我在webbrowser上找到MP3文件时,就会启动一个新的asynctask,并在listview中为每个asynctask启动一个进度条。因此,下载次数可以超过1次,并且可以同时进行。但现在,每当启动Asynctask时,ProgressBar对所有异步任务移动相同,而对不同的Asynctask移动相同,请引导我Android 异步任务文件下载时listview中的进度条,android,listview,progress-bar,android-asynctask,Android,Listview,Progress Bar,Android Asynctask,我遇到了一个奇怪的问题,每当我在webbrowser上找到MP3文件时,就会启动一个新的asynctask,并在listview中为每个asynctask启动一个进度条。因此,下载次数可以超过1次,并且可以同时进行。但现在,每当启动Asynctask时,ProgressBar对所有异步任务移动相同,而对不同的Asynctask移动相同,请引导我 public class CopyOfDownloadsListActivity extends ListActivity { /**
public class CopyOfDownloadsListActivity extends ListActivity {
/** Called when the activity is first created. */
// static ArrayList<String> pthreads = new ArrayList<String>();
ImageView bt;
ProgressBar pb;
ListView allList;
TextView tv;
String fileName;
String mp3URL;
URL url2;
int filecount = 0;
private class DownloadFile extends AsyncTask<String, Integer, Void>{
MyCustomAdapter adapter;
int count = 0;
ProgressDialog dialog;
ProgressBar progressBar;
int myProgress;
@Override
protected Void doInBackground(String... u) {
try {
URL ul = new URL(u[0]);
Log.i("UI",ul.toString());
// int len = CopyOfMusicDownloader.mp3urls.size();
// URL url2 = new URL(CopyOfMusicDownloader.mp3urls.get(len-1));
HttpURLConnection c = (HttpURLConnection) ul.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
int lengthOfFile = c.getContentLength();
String PATH = Environment.getExternalStorageDirectory()
+ "/download/";
Log.v("", "PATH: " + PATH);
File file = new File(PATH);
file.mkdirs();
fileName = "Track";
filecount++;
fileName = fileName + Integer.toString(filecount) + ".mp3";
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
myProgress = (int)((len1/lengthOfFile)*100);
myProgress = myProgress + myProgress;
Log.i("lengthOfFile", Integer.toString(lengthOfFile));
Log.i("My Progress", Integer.toString(myProgress));
publishProgress(myProgress);
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
}catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute() {
}
@Override
protected void onPreExecute() {
adapter = new MyCustomAdapter(CopyOfDownloadsListActivity.this, R.layout.row, CopyOfMusicDownloader.mp3urls);
setListAdapter(adapter);
}
@Override
protected void onProgressUpdate(Integer... values) {
Log.i("Value", values[0].toString());
count++;
adapter.notifyDataSetChanged();
}
public class MyCustomAdapter extends ArrayAdapter<String> {
public MyCustomAdapter(Context context, int textViewResourceId, ArrayList<String> pthreads) {
super(context, textViewResourceId, pthreads);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.row, parent, false);
bt =(ImageView)row.findViewById(R.id.cancel_btn);
tv =(TextView)row.findViewById(R.id.filetext);
pb = (ProgressBar)row.findViewById(R.id.progressbar_Horizontal);
pb.setProgress(count);
return row;
}
}
}
公共类CopyOfDownloadsStactivity扩展了ListActivity{
/**在首次创建活动时调用*/
//静态ArrayList pthreads=newArrayList();
ImageView bt;
ProgressBar-pb;
列表视图所有列表;
文本视图电视;
字符串文件名;
字符串mp3URL;
URL url2;
int filecount=0;
私有类下载文件扩展异步任务{
MyCustomAdapter适配器;
整数计数=0;
进程对话;
ProgressBar ProgressBar;
int-myProgress;
@凌驾
受保护的Void doInBackground(字符串…u){
试试{
URL ul=新URL(u[0]);
Log.i(“UI”,ul.toString());
//int len=CopyOfMusicDownloader.mp3URL.size();
//URL url2=新URL(CopyOfMusicDownloader.mp3URL.get(len-1));
HttpURLConnection c=(HttpURLConnection)ul.openConnection();
c、 setRequestMethod(“GET”);
c、 设置输出(真);
c、 connect();
int lengthOfFile=c.getContentLength();
String PATH=Environment.getExternalStorageDirectory()
+“/下载/”;
Log.v(“,”路径:“+PATH”);
文件=新文件(路径);
mkdirs()文件;
fileName=“Track”;
filecount++;
fileName=fileName+Integer.toString(filecount)+“.mp3”;
File outputFile=新文件(文件,文件名);
FileOutputStream fos=新的FileOutputStream(outputFile);
InputStream=c.getInputStream();
字节[]缓冲区=新字节[1024];
int len1=0;
而((len1=is.read(buffer))!=-1){
我的进度=(整数)((len1/lengthOfFile)*100);
myProgress=myProgress+myProgress;
Log.i(“lengthOfFile”,Integer.toString(lengthOfFile));
Log.i(“我的进度”,Integer.toString(myProgress));
出版进度(myProgress);
fos.写入(缓冲区,0,len1);
}
fos.close();
is.close();
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
受保护的void onPostExecute(){
}
@凌驾
受保护的void onPreExecute(){
adapter=新的MyCustomAdapter(copyOfDownloadsStactivity.this、R.layout.row、CopyOfMusicDownloader.mp3URL);
setListAdapter(适配器);
}
@凌驾
受保护的void onProgressUpdate(整型…值){
Log.i(“Value”,值[0].toString());
计数++;
adapter.notifyDataSetChanged();
}
公共类MyCustomAdapter扩展了ArrayAdapter{
公共MyCustomAdapter(上下文上下文、int textViewResourceId、ArrayList pthreads){
super(上下文、textViewResourceId、pthreads);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutInflater充气机=getLayoutInflater();
视图行=充气机。充气(R.layout.row,父级,false);
bt=(ImageView)row.findViewById(R.id.cancel\u btn);
tv=(TextView)row.findviewbyd(R.id.filetext);
pb=(ProgressBar)row.findViewById(R.id.ProgressBar_水平);
pb.设置进度(计数);
返回行;
}
}
}
这是AsyncTask的开始,以及它的onCreate
新建下载文件().execute(url2.toString());您的问题是这个
静态进度条pb;
不能只有一个静态引用,而希望控制多个进度条。请将进度条完全封装在AsyncTask中,使其成为实例变量
编辑
在onProgressUpdate中,您需要更改ProgressBar的进度。您不需要为每一行都使用适配器,这是一种浪费
@Override
protected void onProgressUpdate(Integer... values) {
Log.i("Value", values[0].toString());
count++;
progressBar.setProgress(count);
}
您也从未分配值progressBar,您一直在评估pb
,去掉该变量!在预执行过程中,您一直在重新分配列表的适配器
您需要进行大量修改。创建一个管理listview的适配器。每行可以有一个AsyncTask,用于维护自己的进度栏、图像视图和文本视图。此外,请阅读此处有关execute()的说明:
蜂巢之后,将只有一个线程运行所有异步任务,因此您可能无法同时运行多个。在第一个线程之后,其余线程将排队,在第一个线程完成之前不会执行。您可能需要在一个线程中执行每个下载,并使用单个异步任务来监视所有线程。(您也可以从线程进行监视,但您需要采取额外的步骤来安全地将更新发布到UI线程上的UI)。感谢您指出这一点,我不知道他们会在默认情况下将其更改回串行池。实际上,您可以有多个异步任务。由于API 11,您可以使用[link](,Params…)