Firebase数据库检索带有键的数据

huangapple go评论82阅读模式
英文:

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 HargaPegawai。我不希望它读起来像这样: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&lt;String&gt; keys = new ArrayList&lt;&gt;();
            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(&quot;tujuan&quot;).getValue(String.class));

huangapple
  • 本文由 发表于 2020年9月9日 12:51:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63804952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定