我使用listview显示下载列表的进度。 我通过在处理程序中调用notifyDataSetChanged来使用线程来每秒更新进度。 一切正常,直到我尝试使用setOnItemClickListener来获取列表中单击的项目。当更新线程运行时,onItemClick的结果是不可预测的,我点击列表中的第一项但是onItemClick的结果显示我点击了第二个。 我认为这是因为点击项目时数据会发生变化,但我该如何防止呢? 我试图在单击项时将isListItemBeenClicked标志设置为true,并且当isListItemBeenClicked为false时更新线程仅调用notifyDataSetChanged,但它不起作用。 任何人都可以帮我吗?
PS:对不起我的蹩脚英语
代码:
1.Refresh handler
listViewRefreshHandler = new Handler() {
@Override
public void handleMessage(Message msg){
if(!isListItemBeenClicked){
System.out.println("start refresh");
downloadingTaskList.clear();
uploadingTaskList.clear();
completedTaskList.clear();
downloadingTaskList.addAll(transferService.getDownloadingTaskList());
uploadingTaskList.addAll(transferService.getUploadingTaskList());
completedTaskList.addAll(transferService.getCompletedTaskList());
Collections.sort(completedTaskList, new Comparator<TransferTask>(){
@Override
public int compare(TransferTask transferTask1, TransferTask transferTask2) {
int ret=0;
long result=transferTask2.getCompletedDate().getTime()-transferTask1.getCompletedDate().getTime();
if(result==0){
ret=0;
}else if(result>0){
ret=1;
}else{
ret=-1;
}
return ret;
}
});
downloadingTaskListAdapter.notifyDataSetChanged();
completedTaskListAdapter.notifyDataSetChanged();
uploadingTaskListAdapter.notifyDataSetChanged();
System.out.println("refresh end");
}
}
};
2.OnItemClickListener
list.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
isListItemBeenClicked=true;
System.out.println("click start");
TransferTask downloadTask=(TransferTask)view.getTag();
System.out.println("click "+downloadTask.getFileName());
System.out.println("click end");
isListItemBeenClicked=false;
}
});
3.更新线程
new Thread() {
public void run() {
while (!isStop) {
try {
Thread.sleep(1000);
listViewRefreshHandler.sendEmptyMessage(1);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}.start();
4.TransferTaskListAdapter
public class TransferTaskListAdapter extends BaseAdapter {
private Context context;
private List<TransferTask> transeferTaskList;
public TransferTaskListAdapter(Context context, List<TransferTask> downloadTaskList ) {
this.context = context;
this.transeferTaskList = downloadTaskList;
}
public int getCount() {
return transeferTaskList.size();
}
public TransferTask getItem(int position) {
return transeferTaskList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("get view called");
TransferTask transferTask=getItem(position);
if(convertView==null){
convertView= LayoutInflater.from(context).inflate(R.layout.transfering_task, null);
}
convertView.setTag(transferTask);
ImageView taskStatusImageView=(ImageView)convertView.findViewById(R.id.taskStatus);
TransferTaskStatus tastStatus=transferTask.getStatus();
switch(tastStatus){
case DOWNLOADING:
taskStatusImageView.setImageResource(R.drawable.icon_status_download);
break;
case UPLOADING:
taskStatusImageView.setImageResource(R.drawable.icon_status_upload);
break;
case PAUSED :
taskStatusImageView.setImageResource(R.drawable.icon_status_paused);
break;
case COMPLETED:
if(transferTask.getType().equals(TaskType.DONWLOAD)){
taskStatusImageView.setImageResource(R.drawable.icon_status_download);
}else{
taskStatusImageView.setImageResource(R.drawable.icon_status_upload);
}
break;
case WAITING:
taskStatusImageView.setImageResource(R.drawable.icon_status_waiting);
break;
case STARTING:
taskStatusImageView.setImageResource(R.drawable.icon_status_waiting);
break;
case STOPPING:
taskStatusImageView.setImageResource(R.drawable.icon_status_waiting);
break;
default:
taskStatusImageView.setImageResource(R.drawable.icon_status_failed);
}
String fileName=transferTask.getFileName();
TextView fileNameTextView=(TextView)convertView.findViewById(R.id.fileName);
fileNameTextView.setText(fileName);
long size=transferTask.getSize();
long transferedSize=transferTask.getTransferedSize();
FileTransferProgressBar progressBar=(FileTransferProgressBar)convertView.findViewById(R.id.downloadProgress);
progressBar.setTotalFileSize(size);
progressBar.setTransferedFileSize(transferedSize);
System.out.println("get view end");
return convertView;
}
}
答案 0 :(得分:0)
尝试使用以下内容:
TransferTask downloadTask = (TransferTask)parent.getItemAtPosition(position);
而不是你当前的:
TransferTask downloadTask=(TransferTask)view.getTag();