I try to do a view to show multiple files download.
ProgressDialog is great to showing single file download info, but i think it can’t be used as element of ListView or somehow set for multiple files indication (or can it?)
So i implement own adapter as shown in some guides like this, and it works ok, i can add Download Items (TextView with name + ProgressBar with max set to file size), but how update ProgressBar at some list item?
If this is a bad practice, which way is best to display multiple files download progress?
download_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/download_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ProgressBar
android:id="@+id/download_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/download_label" />
</RelativeLayout>
Download class
public class Download
{
public String name;
public int size;
public Download(String name, int size)
{
this.name = name;
this.size = size;
}
}
DownloadAdapter class
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;
public class DownloadsAdapter extends ArrayAdapter<Download>
{
private int layoutResourceId;
public DownloadsAdapter(Context context, int textViewResourceId)
{
super(context, textViewResourceId);
this.layoutResourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
DownloadHolder downloadHolder = null;
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) super.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(layoutResourceId, parent, false);
downloadHolder = new DownloadHolder();
downloadHolder.progressBar = (ProgressBar)convertView.findViewById(R.id.download_bar);
downloadHolder.label = (TextView)convertView.findViewById(R.id.download_label);
Log.i("INFO", "HOLDER SET");
convertView.setTag(downloadHolder);
}
else
{
downloadHolder = (DownloadHolder)convertView.getTag();
}
Download download = super.getItem(position);
downloadHolder.label.setText(download.name);
downloadHolder.progressBar.setIndeterminate(false);
downloadHolder.progressBar.setMax(download.size);
return convertView;
}
public static class DownloadHolder
{
ProgressBar progressBar;
TextView label;
}
}
DownloadView class
import android.app.ListActivity;
import android.os.Bundle;
public class DownloadsView extends ListActivity
{
public static DownloadsAdapter DOWNLOADS = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
DOWNLOADS = new DownloadsAdapter(this, R.layout.download_item);
setListAdapter(DOWNLOADS);
//TEST
/*
DownloadsView.addDownload("Lol", 232);
DownloadsView.addDownload("fsfs", 123);
*/
}
public static void addDownload(String name, int size)
{
DOWNLOADS.add(new Download(name, size));
}
}
Ok so here goes…
FileUtils will not be compatible with your files so I will leave it’s implementation to you. But it maps transfers to contacts that sent them. It’s just a utility hashmap helper class 🙂
Heres the XML
incoming_file.xml
transfer_dialog.xml
transfer_item.xml
Transfer dialog code, will need adaptation:
Incoming file dialog:
These rely on FileUtils but with some effort you can swap Conversation out for a String and Transfer out for a file or some sort of wrapper 🙂