How to get data from Firebase Realtime Database and show it in a spinner?

后端 未结 2 1401
悲&欢浪女
悲&欢浪女 2020-12-04 03:19

As title, how to get data from firebase and show in android studio? Currently, because I don\'t know how to use spinner, so I put the id eventSpinner in XML fil

相关标签:
2条回答
  • 2020-12-04 03:46

    I want to show all the registerEventName (Google) of all the eventid (1111,2222,3333....) in the spinner.

    To get all event objects where registerEventName property is equal to Google, please use the following query:

    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    Query query = rootRef
        .child("ListOfEvents")
        .child(uid)
        .orderByChild("registerEventName")
        .equalsTo("Google");
    query.addListenerForSingleValueEvent(/* ... */);
    

    Edit:

    According to your comment, if you want to display all event names into a spinner, please use the following lines of code:

    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference uidRef = rootRef.child("ListOfEvents").child(uid);
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            List<String> eventList = new ArrayList<String>();
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String registerEventName = ds.child("registerEventName").getValue(String.class);
                eventList.add(registerEventName);
            }
            Spinner spinner = (Spinner) findViewById(R.id.spinner);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, eventList);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);
        }
    
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
        }
    };
    uidRef.addListenerForSingleValueEvent(valueEventListener);
    
    0 讨论(0)
  • 2020-12-04 03:57

    I believe all you are looking for is a way to get a list of all titles So what you do is you get the values and store them in an arraylist.

    Then Using an adapter put them in a spinner.

    So here is a code which will fetch all of them as a list form firebase:

    In your onDataChange() Implement something like this.

    //Then pass the list to a method to update the spinner where you use a spinner adapter.
    
         @Override
                public void onDataChange(DataSnapshot snapshot) {
                    if (snapshot != null) {
                        List<String> titles= = new ArrayList<>();
                        for ( DataSnapshot snapshot1:snapshot.getChildren()){
                            MyEvents allocation = snapshot1.getValue(MyEvent.class);
                            // Hope you have a class called Representing the objects I called mine myEvents
                            //Put it in the List
                            titles.add(allocation.getTitle());
                        }
    
                    }
                }
    
    0 讨论(0)
提交回复
热议问题