如何使用Firebase Android在微调器中获取所有子对象

时间:2016-12-01 07:08:10

标签: android firebase firebase-realtime-database

enter image description here

请检查上面的图片, 我想得到" Basit"和" Kashif"来自Android中Firebase的微调器

mdatabase.child("Orders/Particulars").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot areaSnapshot: dataSnapshot.getChildren()) {
                String Particulars = areaSnapshot.child("Basit").getValue(String.class);
                final String[] areas = {Particulars};
                ArrayAdapter<String> areasAdapter = new ArrayAdapter<String>(OrderActivity.this, android.R.layout.simple_spinner_item, areas);
                areasAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                particular.setAdapter(areasAdapter);
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

2 个答案:

答案 0 :(得分:0)

下面的代码将获取所有值的列表...最后在需要的地方将列表添加到微调器中.....

mDatabaseref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            BTLog.d(TAG, "There are " + snapshot.getChildrenCount() + " blog posts");
            isdList.clear();
            isdAdapter = new BTIsdAdapter(isdList);
            isdAdapter.notifyDataSetChanged();
            for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                BTIsdDetails post = postSnapshot.getValue(BTIsdDetails.class);
                BTIsdDetails btIsdDetails = new BTIsdDetails(post.getName(), post.getId());
                isdList.add(btIsdDetails);
                BTLog.d(TAG, " -------------------------- " + post.getName());
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            hideProgressDialog();
            BTLog.d(TAG, "The read failed: ");
        }
    });

BTIsdDetails是pojo类

答案 1 :(得分:0)

因为您需要在微调器中添加键。做以下事情..

mDatabaseref.child("Orders").child("Particulars").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
       for(DataSnapshot postSnapshot : snapshot.getChildren()){
            Log.d("Tag","Key : " + postSnapshot.getKey())
            // make string array of keys
       }
       // add spinner adapter code here
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        hideProgressDialog();
        BTLog.d(TAG, "The read failed: ");
    }
});

希望这会对你有所帮助。