Android firebase orderByKey不会改变任何东西

时间:2016-07-04 08:33:24

标签: java android firebase firebase-realtime-database

我现在正在寻找两天,但我认为firebase的话题很难讨论

我想从firebase DB中对数据进行排序,以获得从最新到最旧的权重。我试图直接使用ref.orderByKeyQuery refQuery = ref.orderByKey();进行此操作但似乎没有效果。

密钥是yy:MM:dd格式的日期,但是使用该代码我可能无法以任何可能的方式对数据进行排序...是否有任何帮助?

screenshot

final Firebase ref = new Firebase("https://myapp.com/" + FirebaseAuth.getInstance().getCurrentUser().getUid() + "/weight/");

ref.orderByKey().addListenerForSingleValueEvent(new ValueEventListener() {
     @Override
     public void onDataChange(DataSnapshot snapshot) {
        weights = new Double[(int)snapshot.getChildrenCount()];
        dates = new String[(int)snapshot.getChildrenCount()];
        Toast.makeText(getActivity(), "Wczytuje Pierwsze Dane"+snapshot.getValue(), Toast.LENGTH_LONG).show();
        int i = 0;
        for (DataSnapshot child : snapshot.getChildren()) {
            weights[i] = child.getValue(Double.class);
            dates[i] = child.getKey();
            weightMap.put(child.getKey(), child.getValue(Double.class));
            i++;
        }
        testowy.setText(""+weightMap.size());

        if(weightMap.size()>1)
        diffValue.setText(""+(weights[weightMap.size()-1] - weights[weightMap.size()-2]));

        WeightListAdapter weightList = new WeightListAdapter(weightMap);
        ListView weightListView = (ListView) view.findViewById(R.id.weightList);
        weightListView.setAdapter(weightList);
     }

     @Override
     public void onCancelled(FirebaseError firebaseError) {
     }
 });

编辑1:

 ref.orderByKey().addListenerForSingleValueEvent(new ValueEventListener() {
                                             @Override
                                             public void onDataChange(DataSnapshot snapshot) {
                                                 weights = new Double[(int)snapshot.getChildrenCount()];
                                                 dates = new String[(int)snapshot.getChildrenCount()];

                                                 int i = 0;
                                                 for (DataSnapshot child : snapshot.getChildren()) {
                                                     weights[i] = child.getValue(Double.class);
                                                     dates[i] = dt.format(Long.parseLong(child.getKey()));
                                                     weightMap.put(child.getKey(), child.getValue(Double.class));
                                                     i++;
                                                 }

我没有转换日期以保持问题的清晰度。我快照未分类。为什么......?

screenshot

编辑2: 这是你问我的吗?

screenshot

2 个答案:

答案 0 :(得分:0)

  • 尝试以毫秒为单位格式化日期,然后将其存储到 数据库中。
  • 最好以毫秒为单位转换日期,然后将其存储到数据库中,然后在从db中检索时将其转换回来。

否则,

  • 如果您将日期设为关键字并且无法更改,则添加 时间作为孩子(可以使用重量等数据)并对该孩子进行排序。按时存储毫秒。
  • 并使用此: - ref.orderByChild("time").addLis...

已更新:

  • 替换此

    final Firebase ref = new Firebase("https://myapp.com/" + FirebaseAuth.getInstance().getCurrentUser().getUid() + "/weight/");
    
    • 用这个

       final Firebase ref = new Firebase("https://myapp.com/" + FirebaseAuth.getInstance().getCurrentUser().getUid() + "/weight/hiper-diet-keeper/UBwsiRUqZrarupNRS/");
      

并使用ref.orderByKey()

答案 1 :(得分:0)

好的,我已经解决了。

Hashmap不保留其项目的顺序。我使用的是LinkedHashMap,现在可以使用了;)