我有一个ListView
使用TaskView
填充了TaskAdapter
个对象(扩展了ArrayAdapter<Task>
。每个TaskView
都附带了TextView
3}}和两个AsyncTask
用于状态更新。
我还有onProgressUpdate()
的子类,其职责是监视正在进行的任务的进度。每当收到进度通知时,它都会将其传递给onProgressUpdate()
方法。 TaskEvent
方法应该根据它收到的TaskView
更新列表中的视图。
我无法获得与使用列表Task
添加的TaskAdapter
对象相对应的public class ProgressMonitor extends AsyncTask<Void, TaskEvent, Void> {
private Task task;
private TaskView view;
private ListView list;
public ProgressMonitor(Task task, ListView list) {
this.task = task;
// I know leaving unchecked casts are a bad practice, but this was a temporary fix
ArrayAdapter<Task> adapter = (ArrayAdapter<Task>) list.getAdapter();
adapter.add(task);
this.view = // get view from adapter that corresponds to task
}
@Override
protected Void doInBackground(Void... params) {
// do stuff in background, send progress using publishProgress(TaskEvent)
return null;
}
@Override
protected void onProgressUpdate(TaskEvent... values) {
TaskEvent e = values[0];
switch (e.getAction()) {
case SOME_STATUS:
// update view, but view is null
break;
case DIFFERENT_STATUS:
// update view in a different way, but view is still null
break;
// etc.
default:
Log.w(TAG, "No action for " + e.getAction());
}
}
}
。
以下是相关课程:
Task task = // ...
new ProgressMonitor(task, list).execute();
该类的使用方式如下:
{{1}}
答案 0 :(得分:0)
每当您更改显示的数据时,您可能需要添加adapter.notifyDatasetChanged()
。
答案 1 :(得分:0)
for getting views of list you have to assign ListView list to your local object that is list
like this and set adapter to listview object then u will be abe to refresh the list in progress update
public ProgressMonitor(Task task, ListView list) {
this.list = list;
//you have to set apater to list view
list.setAdapter(Adptr);
}
@Override
protected void onProgressUpdate(TaskEvent... values) {
TaskEvent e = values[0];
switch (e.getAction()) {
case SOME_STATUS:
// update view, but view is null
adapter.notifyDataSetChanged();
break;
case DIFFERENT_STATUS:
// update view in a different way, but view is still null
break;
// etc.
default:
Log.w(TAG, "No action for " + e.getAction());
}
}