Java 更新ListView中视图的内容
我有一个Java 更新ListView中视图的内容,java,android,listview,android-view,Java,Android,Listview,Android View,我有一个列表视图,它将承载名为任务视图的复合视图,它是任务的可视化表示。后台工作在AsyncTask中完成,TaskView的子项(两个TextViews和一个ProgressView)在onProgressUpdate()中更新。更新完视图后,我调用listView.invalidateViews(),我认为它应该重新绘制它的所有子视图。但是,情况并非如此,因为所有视图都没有更改。在设置了一些断点并观察了各种视图的值之后,我确定它们确实发生了变化,但它们的新值并没有反映在屏幕上 下面是我的异步
列表视图
,它将承载名为任务视图
的复合视图,它是任务的可视化表示。后台工作在AsyncTask
中完成,TaskView
的子项(两个TextView
s和一个ProgressView
)在onProgressUpdate()中更新。更新完视图后,我调用listView.invalidateViews()
,我认为它应该重新绘制它的所有子视图。但是,情况并非如此,因为所有视图都没有更改。在设置了一些断点并观察了各种视图的值之后,我确定它们确实发生了变化,但它们的新值并没有反映在屏幕上
下面是我的异步任务的概要(注意:类和方法名称已经简化)
和TaskView.update()
要使用新数据刷新listview,请调用分配给listview的适配器的notifyDataSetChanged()
方法。对所更改的每个视图使用invalidate()
方法。
另一个窍门是:
更新传递给适配器的数组列表对象(例如,如果要更新的视图位于位置i),请执行以下操作:
list.remove(i);
list.add(i, new object()); // the new object must have the updated content
adapter.notifyDataSetChange();
更新:
让我再次告诉你这个问题:
您有一个包含taskview对象的列表(每行对应一个taskview)
每行有一个进度条和两个文本视图
您想更新一些行吗
解决方案:
protected void onProgressUpdate(Event... values) {
Task task = view.getTask();
task.setProgress() // first change the content of taskView data
task.setStatus()
task.setSecondaryStatus()
// now let the adapter do update the rows
adapter.notifyDataSetChange() // if it dose not work remove task and add to list and then again call notify
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.list_item, parent, false);
Task task = getItem(position);
TextView status = view.findViewById();
status.setText(task.getStatus());
Progressbar pb = view.findViewById();
//....
TextView secondaryStatus = view.findViewById();
secondaryStatus.setText(task.getSecondaryStatus());
return view;
}
根据我从您的问题中了解到的情况,您正在更新textview的文本?那么这就是答案。ListView的TaskView的子项仍然保持不变。他是对的。唯一需要更改的是数据本身,并调用列表适配器的notifyDataSetChanged()。此方法调用使listview再次呈现。不幸的是,这似乎没有任何效果。我在textview上测试了它,它工作正常,当我单击按钮时,textview内容会更改。仍然没有任何更改。但我只是测试了这两种方法并得到了结果,例如,我在每个列表行中放置了一个按钮,当我按下按钮时,该行将被删除,或者该行的textview将被更新。我想你应该发布getView和view.update()函数。这仍然不会改变任何东西:(它是在滚动后更新的吗?我也有类似的问题…@amshuer它在滚动后不会更新。)
public void update() {
// https://github.com/Todd-Davies/ProgressWheel
progressWheel.setProgress(task.getProgress());
if (task.isIndeterminate()) {
progressWheel.spin();
} else {
progressWheel.stopSpinning();
}
// status and secondaryStatus are TextViews
status.setText(task.getStatus());
secondaryStatus.setText(task.getSecondaryStatus());
}
list.remove(i);
list.add(i, new object()); // the new object must have the updated content
adapter.notifyDataSetChange();
protected void onProgressUpdate(Event... values) {
Task task = view.getTask();
task.setProgress() // first change the content of taskView data
task.setStatus()
task.setSecondaryStatus()
// now let the adapter do update the rows
adapter.notifyDataSetChange() // if it dose not work remove task and add to list and then again call notify
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.list_item, parent, false);
Task task = getItem(position);
TextView status = view.findViewById();
status.setText(task.getStatus());
Progressbar pb = view.findViewById();
//....
TextView secondaryStatus = view.findViewById();
secondaryStatus.setText(task.getSecondaryStatus());
return view;
}