我有一些从服务执行的 AsyncTask 类:
public class DownloadTask extends AsyncTask<Void, Integer, Boolean {
@Override
protected Boolean doInBackground(Void... arg0) {
// do something
publishProgress(i);
}
@Override
protected void onProgressUpdate(Integer ... progress) {
if(textView()!=null) getTextView().setText(i + "%");
}
public void setTextView(TextView textView) {
this.textView = textView;
}
}
和 ListAdapter,在方法中getView()
我使用 AsyncTask 的方法在 AsyncTask 中设置 TextView setTextView()
:
public class DownloadTasksAdapter extends BaseAdapter {
private final Activity context;
private DownloadTask[] downloadTasks;
public DownloadTasksAdapter(Activity context, DownloadTask[] downloadTasks) {
this.context = context;
this.downloadTasks = downloadTasks;
}
public int getCount() {
return downloadTasks.length;
}
public DownloadTask getItem(int position) {
return downloadTasks[position];
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getLayoutInflater();
final View rowView = inflater.inflate(R.layout.downloads_listview_row, parent, false);
final TextView progressTxt = (TextView) rowView.findViewById(R.id.downloads_listview_row_progress);
switch (downloadTasks[position].getStatus()) {
case RUNNING:
downloadTasks[position].setTextView(progressTxt);
break;
}
return rowView;
}
}
AsyncTask 从服务执行,但是当我打开包含 ListView 的 Activity 时,AsyncTask 应该更新一些字段,我该怎么做?:(