英文:
Firebase database retrieve data with key
问题
public void retrieveData(){
listener = databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
findViewById(R.id.progressBar).setVisibility(View.GONE);
spinnerDataList.clear();
List<String> keys = new ArrayList<>();
for (DataSnapshot item : dataSnapshot.getChildren()){
keys.add(item.getKey());
spinnerDataList.add(item.getValue().toString());
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
findViewById(R.id.progressBar).setVisibility(View.VISIBLE);
}
});
}
我想检索我的数据,但数据父级上有唯一键,就像这样:https://photos.app.goo.gl/sowxnnJpMgUY6geo9
我想将它设置为微调器列表。我如何只读取 tujuan
值?例如,我想要值 Sebut Harga
和 Pegawai
。我不希望它读起来像这样:https://photos.app.goo.gl/7KCCUbARTZnVQvoM8。
英文:
I want to retrieve my data but there are unique key on data parent like this https://photos.app.goo.gl/sowxnnJpMgUY6geo9
I want to set it to the spinner list. How can I possibly read the tujuan
value only? For example, I want the value Sebut Harga
and Pegawai
. I don't want it read like this https://photos.app.goo.gl/7KCCUbARTZnVQvoM8 .
public void retrieveData(){
listener = databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
findViewById(R.id.progressBar).setVisibility(View.GONE);
spinnerDataList.clear();
List<String> keys = new ArrayList<>();
for (DataSnapshot item : dataSnapshot.getChildren()){
keys.add(item.getKey());
spinnerDataList.add(item.getValue().toString());
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
findViewById(R.id.progressBar).setVisibility(View.VISIBLE);
}
});
}
答案1
得分: 1
根据您的评论:
> 不,详细信息请参考屏幕截图。我不想要 {tujuan=}
为了仅获取 tujuan
属性的值,请将以下代码行进行更改:
spinnerDataList.add(item.getValue().toString());
为:
spinnerDataList.add(item.child("tujuan").getValue(String.class));
英文:
According to your comment:
> No, please refer to the screenshot. I don't want to have {tujuan=}
To get only the value of that tujuan
property, please change the following line of code:
spinnerDataList.add(item.getValue().toString());
to:
spinnerDataList.add(item.child("tujuan").getValue(String.class));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论