我正在下载管理器项目,所以,为了显示所有下载/下载操作,我更喜欢使用ListView来显示我的下载列表。假设我们有多少下载任务,那么所有任务的进度条都必须更新。对于后台下载任务,我创建了一个新类,我将其命名为HttpDownloader
。所以,我将这些进度条传递给这个类的对象。当一个新的对象被添加到我的任务列表中时,我调用HttpDownloader
的构造函数并将新的项目进度条传递给它。事情弄得我是When i add a new object to tasklist and call notifyDataSetChanged of adapter, my list is refreshed, so all progress bar reset to default layout values but HTTPDownloader Thread is running in background successfully.
所以,这是我的问题是,Android的ListView和适配器
1.调用notifyDataSetChanged后,老ListView的对象的引用是自毁?
2.如果是,我该如何保留旧视图的参考?
3.如果没有,请解释我为什么进度条重置为默认值,并且在后台进程强制通过进度条更改进度值时不更改?
HTTPDownloader class
class HttpDownloader implements Runnable {
public class HttpDownloader (String url, ProgressBar progressBar)
{
this.M_url = url;
this.M_progressBar = progressBar;
}
@Override
public void run()
{
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(this.M_url);
HttpResponse response;
try{
response = client.execute(get);
InputStream is = response.getEntity().getContent();
long contentLength = response().getEntity().getContentLength();
long downloadedLen = 0;
int readBytes = 0;
byte [] buffer = new byte [1024];
while ((readBytes = in.read(buffer, 0, buffer.length)) != -1) {
downloadedLen += readBytes;
//Some storing to file codes
runOnUiThread(new Runnable() {
@Override
public void run() {
M_progressBar.setProgress((100f * downloadedLen)/contentLength);
}
});
}
is.close();
} catch (ClientProtocolException e) {
Log.e("HttpDownloader", "Error while getting response");
} catch (IOException e) {
Log.e("HttpDownloader", "Error while reading stream");
}
}
}
AdapterClass
class MyAdapter extends ArrayAdapter<String> {
ArrayList<String> M_list;
public MyAdapter(ArrayList<String> list) {
super(MainActivity.this, R.layout.download_item, list);
this.M_list = list;
}
@Override
public int getCount() {
return this.M_list.size();
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.download_item, parent, false);
ProgressBar bar = (ProgressBar) rowView.findViewById(R.id.progrees);
new Thread (new HttpDownloader(this.M_list.get(position), bar)).start();
return rowView;
}
}
我想保留此参考。当我们调用'notifyDataSetChanged()'时,所有旧的引用都可以改变。我想保留这些参考文献,我该如何保留这些? (我使用ListView编程下载管理器UI,我们应该更改所有进度条的值) –