使用Parse LiveQuery(Android)增加列表?

时间:2017-05-16 11:34:54

标签: java android list parse-platform parse-server

我尝试使用此库实时显示数据库中出现的新对象数量:https://github.com/parse-community/ParseLiveQuery-Android

我正在侦听Notifications类中出现的新对象。每次创建新通知时,如何在我的StringHolder中增加该值?我想,UI是否更新是另一个故事。但我能否至少实时增加该列表?

ParseLiveQueryClient parseLiveQueryClient = ParseLiveQueryClient.Factory.getClient();
ParseQuery < ParseObject > parseQuery = ParseQuery.getQuery(ParseConstants.CLASS_NOTIFICATIONS);
parseQuery.whereEqualTo(ParseConstants.KEY_READ_STATE, false);
SubscriptionHandling < ParseObject > subscriptionHandling = parseLiveQueryClient.subscribe(parseQuery);
subscriptionHandling.handleEvents(new SubscriptionHandling.HandleEventsCallback < ParseObject > () {
 @Override
 public void onEvents(ParseQuery < ParseObject > query, SubscriptionHandling.Event event, ParseObject object) {
  // HANDLING all events
  List<ParseObject> newNotifications = new ArrayList<>();
  newNotifications.add(object);
  Log.w(getLocalClassName(), "New notification found! " + object.toString());
  result.updateBadge(5, new StringHolder(newNotifications.size() + ""));
 }
});

1 个答案:

答案 0 :(得分:1)

我认为每次创建onEvent时都会声明newNotifications变量。尝试将此行放在共享上下文中,例如和activity或singleton:

List<ParseObject> newNotifications = new ArrayList<>();