【问题标题】:Alarm for firebase火力基地警报
【发布时间】:2016-04-12 08:57:46
【问题描述】:

我创建了一个警报系统,供护理人员用来为老年人设置警报。在这个警报中,我如何获得已设置为顶部的最新警报,而不是每次都出现在底部

mListAdapter = new FirebaseListAdapter<notify>(this, notify.class, android.R.layout.two_line_list_item, mRoot) {
        @Override
        protected void populateView(View view, notify notify, int i) {
            ((TextView)view.findViewById(android.R.id.text1)).setText(notify.getCourse() + "Quantity:" + notify.getQuantity() +"," + notify.getRepeat());
            ((TextView)view.findViewById(android.R.id.text2)).setText("Medication set to " +notify.getDate() + " at " + notify.getTime());
        }
    }; listView.setAdapter(mListAdapter);

【问题讨论】:

    标签: android firebase firebase-realtime-database firebaseui


    【解决方案1】:

    请注意,默认情况下,子项按其键的升序检索。如果在您的情况下,密钥是由 Firebase 自动生成的(例如使用 push() 函数),那么它们的值基于时间戳,并且稍后生成的密钥大于之前生成的任何密钥。因此,您创建的最后一个警报显示在列表底部。

    为了颠倒这个顺序,您可以为每个子对象在其名为created_timestamp 的属性下存储值0 - timestamp,其中timestamp 是创建对象时的时间戳。然后,您可以使用函数orderBy('created timestamp') 来检索具有预期顺序的孩子。

    【讨论】:

      【解决方案2】:

      反转数据不是FirebaseListAdapter 可以解决的问题。相反,您可以在数据库查询 (mRoot) 或 Android 视图中解决它。 whole book worth of questions on reversing a query 之前已经回答过了,所以我这里重点反转视图中的项目。

      由于您使用的是ListView

      listView.setStackFromBottom(true);
      

      对于RecyclerView,等效项是:

      manager.setStackFromEnd(true);
      

      【讨论】:

      • 我已经添加了 listView.setStackFromBottom(true),但它仍然没有重新排列我的数据以在顶部显示最新的数据。为什么会这样?
      猜你喜欢
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 2016-08-03
      • 2021-05-04
      相关资源
      最近更新 更多