Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android firebase orderByKey不';我不能改变任何事情_Java_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

Java Android firebase orderByKey不';我不能改变任何事情

Java Android firebase orderByKey不';我不能改变任何事情,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我已经搜索了两天了,但我想firebase的话题讨论得很差 我想对firebase DB中的数据进行排序,以获得从最新到最旧的权重。我试着直接用ref.orderByKey和Query-refQuery=ref.orderByKey()来做但似乎都不起作用 关键是一个日期,格式是yy:MM:dd,但使用该代码,我得到的数据没有以任何可能的方式排序。。。有什么帮助吗 final Firebase ref = new Firebase("https://myapp.com/" + FirebaseA

我已经搜索了两天了,但我想firebase的话题讨论得很差

我想对firebase DB中的数据进行排序,以获得从最新到最旧的权重。我试着直接用
ref.orderByKey
Query-refQuery=ref.orderByKey()来做但似乎都不起作用

关键是一个日期,格式是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) {
     }
 });

编辑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++;
                                                 }
为了澄清问题,我站在那里没有转换日期。快照没有分类。为什么

编辑2: 这就是你要我做的吗

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

  • 如果您已将日期设置为密钥且无法更改,请添加 将时间作为一个孩子(如体重等数据可用),并对该孩子应用排序。按时存储毫秒
  • 并使用以下命令:-
    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()
  • 尝试以毫秒为单位格式化日期,然后将其存储到 数据库
  • 最好以毫秒为单位转换日期,然后将其存储到数据库,然后在从数据库检索时将其转换回数据库
否则,

  • 如果您已将日期设置为密钥且无法更改,请添加 将时间作为一个孩子(如体重等数据可用),并对该孩子应用排序。按时存储毫秒
  • 并使用以下命令:-
    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()
好的,我已经解决了

Hashmap不保持其项的顺序。我改用LinkedHashMap,现在可以用了;)

好的,我已经解决了


Hashmap不保持其项的顺序。我改用LinkedHashMap,现在可以用了;)

谢谢你,这个主意对我来说太完美了,但还是不合适。我马上更新这个问题。(我不能将照片放在评论中,可以吗?)尝试用
ref.orderByKey()
替换为
ref.orderByChild(“时间”)
如果它不起作用,请向我展示你的firebase db快照(结构快照)新手问题-什么是结构快照?我怎样才能得到它?顺便说一句,为什么orderByKey不起作用?我指的是结构的快照。只要转到你的firebase数据库,给我一个你数据库的快照。顺便说一下,
ref.orderByChild(“time”)
不起作用了??对不起,我弄得一团糟。我用“我的应用程序”取代了“hiper diet keeper”,因此它更具可读性。我已经从url中删除了“/weight/”,因此它正是您所说的内容,而且也不起作用。我一点也不明白。我把“orderByKey”,按值,按子项,都是随机的。这不是关键,也不是价值或孩子。谢谢你,这个想法对我来说很完美,但它仍然不合适。我马上更新这个问题。(我不能将照片放在评论中,可以吗?)尝试用
ref.orderByKey()
替换为
ref.orderByChild(“时间”)
如果它不起作用,请向我展示你的firebase db快照(结构快照)新手问题-什么是结构快照?我怎样才能得到它?顺便说一句,为什么orderByKey不起作用?我指的是结构的快照。只要转到你的firebase数据库,给我一个你数据库的快照。顺便说一下,
ref.orderByChild(“time”)
不起作用了??对不起,我弄得一团糟。我用“我的应用程序”取代了“hiper diet keeper”,因此它更具可读性。我已经从url中删除了“/weight/”,因此它正是您所说的内容,而且也不起作用。我一点也不明白。我把“orderByKey”,按值,按子项,都是随机的。它决不是通过键,也不是通过值或子项。