【问题标题】:Refresh listview adapter on GSM notification receive在 GSM 通知接收时刷新 listview 适配器
【发布时间】:2016-04-27 06:56:32
【问题描述】:

假设listview.java 是一个创建适配器并将其分配给列表视图的文件。

我想要的是当我收到一个 GSM 通知时,我想刷新那个 listview 适配器。因此,一旦我收到通知,我的列表视图应该包含新数据。

我正在使用onMessageReceived(String from, Bundle data)这个GcmListenerService的方法。

我可以从 MyGcmListenerService extends GcmListenerService {} 类中调用 listview.java 的某些方法来刷新适配器并分配给 listview 吗?

如果用户已经在列表视图布局中,那么我希望它使用新数据进行更新。

有什么想法吗?

更清晰

在 listview.java 中,我使用来自共享首选项的数据创建了一个适配器。 当我收到 GSM 通知时,我会对共享首选项进行一些更改。现在我希望我的适配器应该使用新的共享首选项重新创建,并且应该分配给列表视图。

现在伙计们?

【问题讨论】:

    标签: java android listview


    【解决方案1】:

    onMessageReceived(String from, Bundle data) 中,您可以尝试执行listviewAdapter.notifyDataSetChanged()。来自docs of notifyDataSetChanged

    通知附加的观察者底层数据已更改,任何反映数据集的视图都应自行刷新。

    【讨论】:

    • 收到通知后,我对共享首选项中的一些值进行了更改。这些共享首选项用于生成适配器。如果我在 onMessageRecieved() 中设置 listviewAdapter.notifyDataSetChanged() ,我的适配器是否会从共享首选项中的值重新生成并分配给 listview?
    • 首先用共享偏好完成你的任务,一旦你准备好在适配器上设置数据,然后只调用 notifyDataSetChanged()。
    • 但是在调用 notifyDataSetChanged() 之后如何自动从共享首选项创建新适配器?
    【解决方案2】:

    你可以像这样在 onRecieve 方法上使用广播接收器

    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);
    

    在活动中,您将不得不实施这样的更改

    @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");
        Log.d("receiver", "Got message: " + message);
    
    //here refresh your listview
    // adater.notifyDatasetChanged();
    
      }
    };
    
    @Override
    protected void onDestroy() {
      // Unregister since the activity is about to be closed.
      LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
      super.onDestroy();
    }
    

    在接收方法上使用 adater.notifydatasetchanged 刷新您的适配器

    【讨论】:

      【解决方案3】:

      在接收推送通知时使用 BroadcatReceiver 刷新列表视图。

      在您的 ListView 活动中添加以下代码,

      ListView 活动

      @Override
          public void onCreate(Bundle savedInstanceState) {
              -------
      
              IntentFilter filter = new IntentFilter(1001);           
              LocalBroadcastManager.getInstance(this).registerReceiver(
                      handlePushNewMessage, filter);
          }
      
          private final BroadcastReceiver handlePushNewMessage = new BroadcastReceiver() {
              @Override
              public void onReceive(Context context, final Intent intent) {
                  // Update list here and refresh listview using adapter.notifyDataSetChanged();          
      
              }
          };
      
      
      
          @Override
          protected void onDestroy() {
             LocalBroadcastManager.getInstance(this).unregisterReceiver(handlePushNewMessage);
              super.onDestroy();
          }
      

      MyGcmListenerService.javaonMessageReceived()

      上添加以下代码
      Intent intent1 = new Intent(1001).putExtra("MESSAGE", message);
      LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
      

      【讨论】:

      • 在我的代码 MyGCMListner.java 中没有 onHandleIntent 方法,我是否需要像 @override protected void onHandleIntent(){ 那样重写那两行}
      • 你能解释一下你提供的解决方案吗?我是菜鸟,它会帮助我快速学习。
      • 发布你的 MyGcmListenerService 类
      • 在 MyGcmListenerService.java 中添加以下代码 onMessageReceived()
      • @Ritzor,你找到解决方案了吗?
      猜你喜欢
      • 1970-01-01
      • 2016-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      • 1970-01-01
      相关资源
      最近更新 更多