我正在开发下载管理器应用程序,我的结构如下:
我有两个包,第一个包是关于实用程序的所有活动和前景。(Activity、ListAdapters 等)第二个是包包括所有后台活动类。(下载线程包括 FTP、HTTP、HTTPS 等)。
此外,为了保留下载文件的详细信息,我定义了如下类:
public class DownloadInfo {
public DownloadInfo(String url, int connection, int progress, boolean status) {
this.M_url = url;
this.M_connection = connection;
this.M_progress = progress;
this.B_status = status;
}
String M_url;
int M_connection;
int M_progress;
boolean B_status;
}
还有ArrayList<DownloadInfo>
那些保持下载/下载文件列表的。为了初始化 ListView,我声明了继承自的类,ArrayAdapter<DownloadInfo>
如下所示:
public class FillDownloadList extends ArrayAdapter<DownloadInfo> {
private ArrayList<DownloadInfo> M_dlList;
private Context M_context;
private DownloadViewHolder holder;
public FillDownloadList(Context context, ArrayList<DownloadInfo> dlList) {
super(context, R.layout.download_item, dlList);
this.M_dlList = dlList;
this.M_context = context;
}
public View getView(final int position, View convertView,
android.view.ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.download_item, parent,
false);
holder = new DownloadViewHolder();
holder.activityButton = ((Button) convertView
.findViewById(R.id.dl_button));
holder.linkText = ((TextView) convertView
.findViewById(R.id.link_textview));
holder.speedText = ((TextView) convertView
.findViewById(R.id.speed_textview));
holder.percentBar = ((ProgressBar) convertView
.findViewById(R.id.dl_progressbar));
convertView.setTag(holder);
} else {
holder = (DownloadViewHolder) convertView.getTag();
}
holder.linkText.setText(this.M_dlList.get(position).M_name);
holder.percentBar.setProgress(this.M_dlList.get(position).M_progress);
// Doing some initializing and setting the download thread based on DownloadInfo `B_status` parameter.
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("log", M_dlList.get(position).M_url);
Log.e("log", System.out.println(holder.linkText.getText());
}
});
return convertView;
}
private static class DownloadViewHolder {
TextView linkText;
TextView speedText;
Button activityButton;
ProgressBar percentBar;
}
}
为了提高适配器的性能,我声明DownloadViewHolder
类以保留引用(以避免不必要的膨胀)。当我单击 ListView 的项目时,LogCat 输出对我来说是无法理解的:-/!因为第二个Log.e
总是打印添加到 listView 的最后一个项目。怎么了?
有人可以指导我解决吗?
提前致谢 :)