[英]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.orderByKey
和Query 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:这是你问我的吗?
Otherwise, 除此以外,
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.