I want to list all unique childrens and keys along with values in a spinner firebase [closed]

送分小仙女□ 提交于 2020-01-01 03:42:09

问题


I have my realtime database as this. Users=>UID=>Medicines=>Date=>Medicine Name :Values

I have a spinner

 ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arrayList);
    arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
    spinner.setAdapter(arrayAdapter);

I tried this:

mUserDatabase=FirebaseDatabase.getInstance().getReference().child("Users").child(uid).child("Medical");


    // GET ALL DATES // GET ALL MEDICINE NAME // GET ALL Values //
    mUserDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            Log.v("DATES : ",""+ dataSnapshot.getKey());

            for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {

                Log.v("Medicine Name",""+ childDataSnapshot.getKey());

                arraList.add(childDataSnapshot.getKey()); 

                Log.v("VALUES",""+ childDataSnapshot.child(childDataSnapshot.getKey()).getValue());   
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

BUT IT IS EMPTY AND I AM UNABLE TO READ ANY OF THEM.


回答1:


You have written Medical instead of "Medicines".

You can either delete or leave this as solved as rest looks correct

Also childSnapshot.getKey() will give you DATES and childSnapshot.getValue() will give you entire thing inside date

Also If I got you then you can try this:

  mUserDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {

             //"DATES:"+dataSnapshot.getKey()

                for (DataSnapshot childDataSnapshot2 : childDataSnapshot.getChildren()) {

             // "MEDICINE NAME"+childDataSnapshot2.getKey()

                    arrayList.add(childDataSnapshot2.getKey());

             //"VALUES"+childDataSnapshot2.getValue()

                }
                }

        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


来源:https://stackoverflow.com/questions/59499322/i-want-to-list-all-unique-childrens-and-keys-along-with-values-in-a-spinner-fire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!