我现在正在寻找两天,但我认为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: 这是你问我的吗?
答案 0 :(得分:0)
否则,
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,现在可以使用了;)