【问题标题】:need to press back button to see the updated ListView in realtime需要按返回按钮才能实时查看更新的 ListView
【发布时间】:2016-12-12 09:55:06
【问题描述】:

有一个自定义的ListView 使用 PHP 从服务器填充数据并且工作正常。我有一个notifyDataSetChanged 用于列表更新。但问题是当服务器上的数据发生变化时,我需要按返回按钮才能查看ListView 中的更新数据。有没有更好的方法来显示类似于 WhatsApp 列表的更新列表。

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_seconds);

    contlist = (ListView)findViewById(R.id.contlist);
    startService(new Intent(this, serv.class));

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    if(!prefs.getBoolean("firstTime", false)) {
        try {
            getNumber(seconds.this.getContentResolver());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean("firstTime", true);
        editor.commit();
    } else {
        musers = (ArrayList<mobstat>) mobstat.listAll(mobstat.class);
        descAdapter = new DescAdapter(seconds.this, musers, seconds.this);
        contlist.setAdapter(descAdapter);
     }

    intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_TIME_TICK);

    seconds.this.registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            descAdapter.notifyDataSetChanged();
        }
    }, intentFilter);
}

我在OnCreate 方法中为notifyDataSetChanged 使用intentfilter。我也将意图过滤器代码放在OnStartOnResume 方法中,但我需要单击后退按钮才能查看更新的列表。有更好的方式实时显示更新列表吗?

【问题讨论】:

  • 所以你想让我在 onBackPressed 方法中使用 Intentfilter 代码?
  • 是的,取决于您的要求
  • 两个选项,第一个选项是 fcm。每当服务器上发生某些事情时,发送推送通知并检查您是否在要更新和刷新的页面上。另一种选择是 xmpp 或类似 xmpp 协议。

标签: android listview notifydatasetchanged


【解决方案1】:

如果服务器发生某些更改,您必须每次都刷新数据。但是您可以实现Swipe to Refresh 模式来实现它,而无需按下返回按钮。

建议你去this link一个基本的例子吧!

【讨论】:

  • 嘿,我知道刷卡刷新,但我需要实时更新列表视图。谢谢。
  • @user2269164 没关系!只是想告诉你选择!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-16
  • 2022-08-19
  • 2017-07-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多