【发布时间】: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。我也将意图过滤器代码放在OnStart、OnResume 方法中,但我需要单击后退按钮才能查看更新的列表。有更好的方式实时显示更新列表吗?
【问题讨论】:
-
所以你想让我在 onBackPressed 方法中使用 Intentfilter 代码?
-
是的,取决于您的要求
-
两个选项,第一个选项是 fcm。每当服务器上发生某些事情时,发送推送通知并检查您是否在要更新和刷新的页面上。另一种选择是 xmpp 或类似 xmpp 协议。
标签: android listview notifydatasetchanged