[英]Update ListView Row on data change
有人可以帮助我使用回调/侦听器更新ListView
的ProgressBar
吗?
我对 Google 教程的尝试了解不够。 我看过很多“使用 ProgressBar 下载教程”,但它们不适合我正在研究的内容。 我很沮丧,因为如果列表视图和异步任务在同一个类中,那么能够更新ListView
的简单ProgressBar
会很简单。 但是在我的实现中, ListView
只是数据的观察者,它应该监视吗? 被通知? 设置一个计时器并通过notifiyDataSetChanged()
不断刷新ListView
?
我的数据在不同的类中更新。 我的ListView
Activity 仅查看该数据并应显示进度。
ListView
非常适合在加载 Activity 时显示数据的CURRRENT状态。 但未能继续更新ProgressBar
s。
数据类在更新我的对象的进度属性方面效果很好。
现在我只需要在数据更改时更新每一行。
当显示的数据发生更改时,如何让每个ListView
行得到通知?
您可以考虑在您的情况下使用LocalBroadcastReceiver
。 当数据的状态从另一个类更新时,您可能会考虑向持有ListView
的Activity
发送广播事件,并在收到广播时更新ListView
。
要在包含ListView
Activity
或Fragment
实现广播接收器,您需要执行以下操作。
@Override
public void onCreate(Bundle savedInstanceState) {
...
// Register to receive messages.
// We are registering an observer (mMessageReceiver) to receive Intents
// with actions named "custom-event-name".
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("custom-event-name"));
}
// Our handler for received Intents. This will be called whenever an Intent
// with an action named "custom-event-name" is broadcasted.
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
// Do something with message
yourAdapter.notifyDataSetChanged(); // notify the adapter here
}
};
@Override
protected void onDestroy() {
// Unregister since the activity is about to be closed.
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
onReceive
函数将获得已发送广播的回调,并将使用传递给它的当前状态更新ListView
。 我只是在示例中发送消息。 您可以考虑传递任何自定义数据。
现在要从另一个类发送ListView
显示的数据状态,您需要将广播发送到您的Activity
或Fragment
注册的接收器。
// Send an Intent with an action named "custom-event-name". The Intent sent should
// be received by the ReceiverActivity.
private void sendMessage() {
Log.d("sender", "Broadcasting message");
Intent intent = new Intent("custom-event-name");
// You can also include some extra data.
intent.putExtra("message", "This is my message!");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
对数据进行任何更新时调用sendMessage
函数以相应地更新ListView
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.