簡體   English   中英

在數據更改時更新 ListView Row

[英]Update ListView Row on data change

有人可以幫助我使用回調/偵聽器更新ListViewProgressBar嗎?

  • 指出我正確的方向?
  • 告訴我我正在嘗試做什么被稱為?

我對 Google 教程的嘗試了解不夠。 我看過很多“使用 ProgressBar 下載教程”,但它們不適合我正在研究的內容。 我很沮喪,因為如果列表視圖和異步任務在同一個類中,那么能夠更新ListView的簡單ProgressBar會很簡單。 但是在我的實現中, ListView只是數據的觀察者,它應該監視嗎? 被通知? 設置一個計時器並通過notifiyDataSetChanged()不斷刷新ListView

我的數據在不同的類中更新。 我的ListView Activity 僅查看該數據並應顯示進度。

ListView非常適合在加載 Activity 時顯示數據的CURRRENT狀態。 但未能繼續更新ProgressBar s。

數據類在更新我的對象的進度屬性方面效果很好。

現在我只需要在數據更改時更新每一行。

當顯示的數據發生更改時,如何讓每個ListView行得到通知?

您可以考慮在您的情況下使用LocalBroadcastReceiver 當數據的狀態從另一個類更新時,您可能會考慮向持有ListViewActivity發送廣播事件,並在收到廣播時更新ListView

要在包含ListView ActivityFragment實現廣播接收器,您需要執行以下操作。

@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顯示的數據狀態,您需要將廣播發送到您的ActivityFragment注冊的接收器。

// 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM