gpt4 book ai didi

java - Android firebase orderByKey 不会改变任何东西

转载 作者:太空宇宙 更新时间:2023-11-04 12:27:58 25 4
gpt4 key购买 nike

我已经搜索了两天了,但我猜 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

最佳答案

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

否则,

  • 如果您已将日期设置为 key 且无法更改,请添加作为一个 child 的时间(其中体重等数据可用)并对该 child 应用排序。按毫秒存储时间。
  • 并使用这个:- 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()

关于java - Android firebase orderByKey 不会改变任何东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38180127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com