简体   繁体   中英

Update ListView Row on data change

Can someone help me update a ProgressBar in a ListView using a callback/listener?

  • Point me in the right direction?
  • Tell me what I am trying to do is called?

I don't know enough about what I am trying to do to Google a tutorial. I've looked at plenty of "Download Tutorials with ProgressBar" but they don't fit what I am working on. I'm frustrated because being able to update a simple ProgressBar in a ListView would be simple, if the List view and the Async task where in the same class. But in my implementation, the ListView is merely an observer of the data and it should watch? be notified? Set a timer and just constantly refresh the ListView via notifiyDataSetChanged() ?

My data is updated in a different class. My ListView Activity only looks at that data and should display the progress.

The ListView works great at showing the CURRRENT state of the data when the Activity is loaded. But fails to continue to update the ProgressBar s.

The data class works great at updating my object's progress property.

Now I just need to make each row update when the data has changed.

How do I get each ListView row to be notified when the data it is displaying has changed?

You might consider using a LocalBroadcastReceiver in your case. When the state of the data is updated from another class, you might consider sending a broadcast event to the Activity which is holding the ListView and update the ListView when the broadcast is received.

To implement a broadcast receiver in your Activity or Fragment which is holding the ListView you need to do something like this.

@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();
}

The onReceive function will get the callback of the broadcast sent and will update the ListView with the current status passed to it. I'm just sending a message in the example. You might consider passing any custom data.

Now to send the state of the data showed in the ListView from another class, you need to send the broadcast to the receiver registered in your Activity or 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);
}

Call the sendMessage function on any update of your data to update the ListView accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM