微调框无法从Firebase数据库检索任何数据

时间:2018-10-02 09:52:36

标签: android firebase firebase-realtime-database

我想进行微调验证,如果用户未选择任何补习中心名称,它将吐出一条消息。然后,我想用Firebase数据库中的数据填充微调框。如本link中所述。我想在我的编码应用中尝试一下。但是它没有从我的Firebase数据库中检索任何数据。已经过了几天,我还是想不通,有人可以帮我吗?...

我的firebase数据库如下图所示:-

enter image description here

旋转编码:-

   DatabaseReference mDatabaseRef = FirebaseDatabase.getInstance().getReference();

    mDatabaseRef.child("Advertisement").child(mAuth.getUid())
            .addValueEventListener(new ValueEventListener()
            {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot)
                {
                    //ArrayList<String> identity = new ArrayList<>();
                    final List<String> identity = new ArrayList<String>();
                    identity.add(0, "Choose Tuition Centre");

                    for (DataSnapshot snapshot : dataSnapshot.getChildren())
                    {
                        Advertisement advertisement = snapshot.getValue(Advertisement.class);
                        identity.add(advertisement.getAdstuitioname());
                    }

                    ArrayAdapter<String> nameAdapter = new ArrayAdapter<String>(RatingActivity.this, android.R.layout.simple_spinner_item, identity);
                    nameAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    mName.setAdapter(nameAdapter);
                }

                @Override
                public void onCancelled(DatabaseError databaseError)
                {

                }
            });

    mName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
        {
            if(position==0)
            {
                //Toast.makeText(getApplicationContext(),"No Item Selected",Toast.LENGTH_LONG).show();
            }

            else
            {
                Toast.makeText(getApplicationContext(),parent.getItemAtPosition(position) +" Selected",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent)
        {
            // TODO Auto-generated method stub
        }
    });

我的应用程序输出界面:-

enter image description here

1 个答案:

答案 0 :(得分:1)

您的代码中有嵌套ID,实际数据在2个ID之后。您需要遍历。只需将for循环的代码替换为此代码即可。

 for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
     for (DataSnapshot dataSnapshot1 : snapshot.getChildren()) {
      identity.add(dataSnapshot1.child("adstuitioname").getValue(String.class));
         }
     }

保持原样。