gpt4 book ai didi

android - ListView 和 ReferenceHolder

转载 作者:行者123 更新时间:2023-11-30 04:02:47 24 4
gpt4 key购买 nike

我正在开发下载管理器应用程序,我的结构如下:

我有两个包,第一个是关于实用程序的所有 Activity 和前景。 ( Activity 、ListAdapters 等)第二个是包含所有后台 Activity 类的包。 (下载线程包括 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 的最后一项。怎么了?

谁能指导我解决?

提前致谢:)

最佳答案

你的代码有问题,当你点击 happends 时,点击监听器拾取 holder,但它是上次查看列表项的 holder,为了防止它,你必须在 getView() 中创建 holder 并将它传递给 onClickListener 通过构造函数。

关于android - ListView 和 ReferenceHolder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12228636/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com