【问题标题】:How to sort values using dates as keys on Firebase如何在 Firebase 上使用日期作为键对值进行排序
【发布时间】:2018-08-27 17:13:16
【问题描述】:

我正在尝试制作一个待办事项列表,每次用户选中一个复选框时,它都会在相应的日期添加一个真/假。问题是firebase不排序日期字符串,所以经过一些研究我发现我必须使用timeInMillis,但Firebase不允许字符串以外的键......

这就是我检索日期的方式..

habitosQuery.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            mHabitosList.clear();
            for (DataSnapshot habitosSnapshot : dataSnapshot.getChildren()) {
                Habitos habitos = habitosSnapshot.getValue(Habitos.class);

                Checks = habitos.getChecks();
                Map<String, Boolean> sortedChecks = new TreeMap<>(Checks);
                List<Map.Entry<String, Boolean>> entryList =
                        new ArrayList<>(sortedChecks.entrySet());
                Map.Entry<String, Boolean> lastEntry =
                        entryList.get(entryList.size() - 1);

                try {
                    lastDateEntry = sdf.parse(lastEntry.getKey());

                } catch (ParseException e) {
                    e.printStackTrace();
                }
                Date today = focus.getToday();
                int diffLastToday = focus.daysBetween(lastDateEntry, today);

                for (int x = 0; x < diffLastToday; x++) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(lastDateEntry);
                    calendar.add(Calendar.DAY_OF_MONTH, +1);
                    Checks.put(sdf.format(calendar.getTimeInMillis()), false);
                }

                habitos.setChecks(Checks);
                mHabitosList.add(habitos);
                mHabitosAdapter.notifyDataSetChanged();
                habitosQuery.child(habitos.getHabitosId()).child("checks").setValue(Checks);

            }

【问题讨论】:

  • 我并不真正理解使用时间(以毫秒为单位)作为键的问题。不能用字符串来表示吗?
  • 你是对的,你只是在long值的末尾添加一个+“”,但是现在另一个问题突然出现了,你如何获得更新值的特定键?,因为时间戳是不断变化的

标签: java android firebase


【解决方案1】:

所以我设法做到了,这就是我的做法...... 首先你必须在毫秒内得到你的日子,我为此创建了这个方法:

public String[] getPreviousFiveDays()  {    
Calendar cal = Calendar.getInstance();
        String dates[] = new String[5];
        cal.add(Calendar.DAY_OF_MONTH, 0 );
        dates[0] = cal.getTimeInMillis()+"";
        for (int i = 1; i < 5; i++){
            cal.add(Calendar.DAY_OF_MONTH,-1);
            dates[i] = cal.getTimeInMillis()+"";
        }
        return dates;
    }

然后因为我想保存和修改日期,所以我在对象上创建了一个 HashMap..

   private Map<String, Boolean> mChecks;
public Map<String, Boolean> getChecks() {
    return mChecks;
}

public void setChecks(Map<String, Boolean> checks) {
    mChecks = checks;
}

然后为了修改 CheckBox 被选中的日期,我检索了这样的键(因为我只使用了最后五个键,所以我只检索了那些):

 mChecks = new TreeMap<>(habitos.getChecks());

        List<Map.Entry<String, Boolean>> entryList =
                new ArrayList<>(mChecks.entrySet());
        for (int x=0; x<5; x++) {
            int y = 1 + x;
            Map.Entry<String, Boolean> entry =
                    entryList.get(entryList.size() - y);
            mDays[x] = entry.getKey();

        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 2018-06-26
    • 2020-03-17
    • 2011-10-10
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    相关资源
    最近更新 更多