Maison > Questions et réponses > le corps du texte
上面图片中展示的是通过ListView加载一些下载列表,现在遇到一个问题:当点击5035
这条数据下载时,把列表滚到第二页相应位置的2794113
这条数据也会跟着改变,后面每一页的对应位置的数据都会这样,请问该怎么解决?
代码如下:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if(convertView == null){
viewHolder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.activity_down_site_item, null);
viewHolder.siteText = (TextView) convertView.findViewById(R.id.site_name);
viewHolder.siteBar = (ProgressBar) convertView.findViewById(R.id.site_bar);
viewHolder.siteDownload = (Button) convertView.findViewById(R.id.site_download);
viewHolder.siteDelete = (Button) convertView.findViewById(R.id.site_delete);
viewHolder.siteUpdate = (Button) convertView.findViewById(R.id.site_update);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
//从集合中获取数据
JSONObject jsonObject = (JSONObject) mJsonArray.get(position);
final ViewHolder currentViewHolder = viewHolder;
viewHolder.siteText.setText(jsonObject.get("ID").toString());
viewHolder.siteDownload.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
currentViewHolder.siteDownload.setText("等待中");
currentViewHolder.siteDownload.setClickable(false);
currentViewHolder.siteBar.setVisibility(View.VISIBLE);
//TODO 下载逻辑
}
});
if(isDone==1){
//隐藏删除、更新按钮
viewHolder.siteDownload.setVisibility(View.VISIBLE);
viewHolder.siteDelete.setVisibility(View.GONE);
viewHolder.siteUpdate.setVisibility(View.GONE);
}else{
//隐藏下载按钮
viewHolder.siteDownload.setVisibility(View.GONE);
viewHolder.siteDelete.setVisibility(View.VISIBLE);
viewHolder.siteUpdate.setVisibility(View.VISIBLE);
}
return convertView;
}
阿神2017-04-17 14:54:13
复用的问题
viewHolder.siteDownload.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
currentViewHolder.siteDownload.setText("等待中");
currentViewHolder.siteDownload.setClickable(false);
currentViewHolder.siteBar.setVisibility(View.VISIBLE);
//TODO 下载逻辑
}
});
分析错误原因:
点击后设置文本为“等待中”
滑动时复用之前convertView的siteDownload文本和siteBar的Visibility。
另外isDone为成员变量,这里很容易出问题
推荐的数据集实现方式
Sample Bean:
int status; // 未开始下载,下载中,已完成下载 三个状态
int progress; // 进度
String downloadID; // 下载ID
...etc.
根据status 设置对应控件显示。伪代码如下:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item_picking_task, null);
holder = new ViewHolder();
initView(holder, convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
initListener(position, holder); // 监听
fillData(position, holder);
return convertView;
}
private void initView(ViewHolder holder, View convertView) {
holder.siteText = (TextView) convertView.findViewById(R.id.site_name);
holder.siteBar = (ProgressBar) convertView.findViewById(R.id.site_bar);
holder.siteDownload = (Button) convertView.findViewById(R.id.site_download);
holder.siteDelete = (Button) convertView.findViewById(R.id.site_delete);
holder.siteUpdate = (Button) convertView.findViewById(R.id.site_update);
}
private void initListener(int position, final ViewHolder holder) {
final Bean bean = (Bean) mDataList.get(position);
holder.siteDownload.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
改变状态 bean.status = 1;
Adapter.this.notifyDataSetChanged(); // 刷新
}
});
}
private void fillData(int position, ViewHolder holder) {
Bean bean = (Bean) mDataList.get(position);
holder.siteText.setText(bean.downloadID);
holder.siteDownload.setText(bean.status); // 伪代码 此处应为stats对应的String类型文本
holder.siteDownload.setClickable(bean.status != 1);
holder.siteBar.setVisibility(bean.status == 1 ? View.VISIBLE : View.GONE);
}
private class Bean implements Serializable {
int progress;
int status; // 未开始: 0,下载中: 1,已完成: 2
String downloadID;
...
}
若还有问题,请追问