简体   繁体   English

Android firebase orderByKey不会改变任何东西

[英]Android firebase orderByKey doesn't change anything

I was searching for two days now, but the firebase topic is really poorly discussed i guess 我现在正在寻找两天,但我认为firebase话题的讨论很少

I want to sort my data from firebase DB to get the weights from the newest to the oldest. 我想从firebase数据库中对数据进行排序,以获得从最新到最旧的权重。 I was trying to do it directly with ref.orderByKey and with Query refQuery = ref.orderByKey(); 我试图直接使用ref.orderByKeyQuery refQuery = ref.orderByKey(); but none seems to work. 但似乎没有工作。

The key is a date with yy:MM:dd format, but with that code i'm getting data not sorted in any possible way... Any help? 关键是yy:MM:dd格式的日期,但是使用该代码我得到的数据没有以任何可能的方式排序......任何帮助?

截图

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) {
     }
 });

Edit 1: 编辑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++;
                                                 }

I stood without converting the date for clarity of the question.The snapshot comes unsorted. 为了清晰起见,我站在没有转换日期的情况下。快照未分类。 Why...? 为什么...?

截图

Edit 2: Is this what you asked me for? 编辑2:这是你问我的吗?

截图

  • Try to format your date in milliseconds and then store it to database. 尝试以毫秒为单位格式化日期,然后将其存储到数据库。
  • Its better to convert your date in millisecond and then store it to the database and then convert it back when retrieved from db. 最好以毫秒为单位转换日期,然后将其存储到数据库中,然后在从db检索时将其转换回来。

Otherwise, 除此以外,

  • If you have set date as your key and can't change it then add time as a child (where data like weight is available) and apply sort on that child. 如果您将日期设置为您的密钥并且无法更改它,那么请将时间添加为子项(可以使用权重等数据)并对该子项应用排序。 Store millisecond on time. 按时存储毫秒。
  • And use this :- ref.orderByChild("time").addLis... 并使用: - ref.orderByChild("time").addLis...

Updated : 更新 :

  • Replace this 替换它

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

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

and use ref.orderByKey() 并使用ref.orderByKey()

Ok, i've solved it. 好的,我已经解决了。

Hashmap doesn't keep order of it's items. Hashmap不保留其项目的顺序。 I used LinkedHashMap instead and it works now ;) 我使用的是LinkedHashMap,现在可以正常工作;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM