I am newbie on android programming, now I met a wired thing on it, I created a listActivity, and the layout of list item is like following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="15dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/transparent" >
<ImageView
android:id="@+id/offlineItemIcon"
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:scaleType="fitXY"
android:src="@drawable/user_avatar" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/offlineItemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:text="卷001 2012-08-23 3.0M"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/lightdark"
android:textSize="18sp" />
<ProgressBar
android:id="@+id/offlineLoadingbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="5dp"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp"
android:max="100"
android:progress="10" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center" >
<Button
android:id="@+id/buttonOfflineItemDownload"
android:layout_width="60dp"
android:layout_height="44dp"
android:background="@drawable/xml_button_loadmore"
android:text="download"
android:textColor="@color/lightwhite"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@color/commentDate" >
</LinearLayout>
</LinearLayout>
I made a class to wrap the content of this layout, this one –
public class OfflineItemView extends LinearLayout {
Context _context = null;
public Button buttonDownload = null;
public ProgressBar loadingProgress = null;
public TextView title = null;
public ImageView icon = null;
public String url = null;
public String filename = null;
public OfflineItemView(Context context) {
super(context);
_context = context;
init();
}
public OfflineItemView(Context context, AttributeSet attrs) {
super(context, attrs);
_context = context;
init();
}
protected void init() {
LayoutInflater inflater = (LayoutInflater) _context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.module_offline_item, this);
buttonDownload = (Button) findViewById(R.id.buttonOfflineItemDownload);
buttonDownload.setTag("download");
loadingProgress = (ProgressBar) findViewById(R.id.offlineLoadingbar);
loadingProgress.setMax(100);
loadingProgress.setProgress(0);
title = (TextView) findViewById(R.id.offlineItemTitle);
icon = (ImageView) findViewById(R.id.offlineItemIcon);
}
public void setTitle(String message) {
if (message != null) {
title.setText(message);
} else {
title.setText("");
}
}
public void setImageBitmap(Bitmap bitmap) {
if (bitmap != null) {
icon.setImageBitmap(bitmap);
}
}
public void setListener(View.OnClickListener listener) {
this.buttonDownload.setOnClickListener(listener);
}
public void setProgress (int i)
{
android.util.Log.v ("item", i + "");
loadingProgress.setProgress(i);
android.util.Log.v ("item", "current : " + loadingProgress.getProgress());
}
}
The list working pretty good, it display all item without problem, the screenshot of the list item is –

When the user clicked the button, it will invoke an AsyncTask class to download a file as well as display the progress on item, the download class –
public void downloadOffline(String url, String path,
OfflineItemView progress) {
DownloadOfflineAsync downloader = new DownloadOfflineAsync(progress);
downloader.execute(url, path);
}
class DownloadOfflineAsync extends AsyncTask<String, String, String> {
private final WeakReference<OfflineItemView> progressbarReference;
public DownloadOfflineAsync(OfflineItemView progress) {
progressbarReference = new WeakReference<OfflineItemView>(progress);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
android.util.Log.v("downloadFile", "Lenght of file: "
+ lenghtOfFile + ":" + aurl[1]);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(aurl[1]);
try {
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""
+ (int) ((total * 100) / lenghtOfFile));
output.write(data, 0, count);
}
} finally {
if (output != null) {
output.flush();
output.close();
}
if (input != null) {
input.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate(String... progress) {
if (progressbarReference != null) {
OfflineItemView p = progressbarReference.get();
//android.util.Log.v ("offline.download", progress[0]);
p.setProgress(Integer.parseInt(progress[0]));
}
}
@Override
protected void onPostExecute(String unused) {
OfflineItemView p = null;
if (progressbarReference != null) {
p = progressbarReference.get();
p.buttonDownload.setTag("browser");
p.buttonDownload.setText("browse");
p.requestLayout();
}
}
}
And it’s working well, the file is downloaded, and progress is notified to onProgressUpdate, also the loading progress bar on item class is working fine, the progress is set correctly (to print on logcat, and all correct), but the problem is the progress bar never update its indicator, so even the progress is updated from 0 to 100 increasingly, the progress bar doesn’t change anything to show the progress on its UI, so anyone who can give me some advices are appreciated..
PS:
I move the download function to getView (), and seems it’s working great, the progress bar is updating, but in the button event handler is not working, so seems it’s list view problem, how to solve this problem, but?
@Override
public View getView(int position, View convertView, ViewGroup parent) {
rowView = new OfflineItemView(_context);
JSONObject item = getItem(position);
try {
if (item != null) {
String t = item.getString(Offline.TAG_NAME) + " "
+ item.getString(Offline.TAG_DATE) + " "
+ item.getString(Offline.TAG_SIZE);
rowView.setTitle(t);
if (listener == null) {
listener = new ItemButtonOnClickListener();
}
if (item.optBoolean("dir_exists")) {
rowView.buttonDownload.setText("browse");
rowView.buttonDownload.setTag("browser");
}
rowView.setListener(listener);
rowView.url = item.getString(Offline.TAG_URL);
rowView.filename = item.getString(Offline.TAG_FILENAME);
String imageUrl = item.optString(Offline.TAG_ICON);
if (imageUrl == null || imageUrl.trim().equals("")) {
} else {
String name = item.optString(Offline.TAG_UNZIP_NAME) + "o";
if (name != null) {
if (DiskCache.getInstance().fileExists("user_" + name,
"user")) {
android.util.Log.v("offline", name + ":exists");
Bitmap bitmap = DiskCache.getInstance().get(
"user_" + name, "user");
if (bitmap != null) {
rowView.setImageBitmap(bitmap);
}
} else {
// FileDownloader.getInstance().download(imageUrl,
// image, name);
}
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return rowView;
}
You’re approaching this in kind of a curious way. You said you’re new to Android, and I can’t tell from your comment thread above what your current state of brokenness is. However, I can tell you you’re not following
ListViewbest-practices, and that you might run into some strange issues when you scroll down your list.You should use the
convertViewparameter you’re given.convertViewis either null, or aViewwhich you have previously returned fromgetView. Since you are only ever returningOfflineItemViews, ifconvertView != nullit’s going to be an instance ofOfflineItemView, which you can reuse to avoid the overhead of creating a completely new one at draw-time every time a new item is displayed.Basically:
Just be careful with logic like this, because the next time you re-use the view the tags will still be there from the last item that occupied it:
I.E., you may need an
elseclause to sayrowView.buttonDownload.setText("");and nullify the tag in the event thatdir_existsis false.Additionally, (assuming you mean to use something like the commented-out
ImageDownloadercode you have ingetView) if the image doesn’t exist in your image cache, you may spawn off multiple downloads of the same image ifgetViewis called more than once before the image is actually downloaded.If you can edit your question to clarify precisely what is broken right now, I can refine this answer to help you further.