我正在下載管理器項目,所以,爲了顯示所有下載/下載操作,我更喜歡使用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,我們應該更改所有進度條的值) –