2

Hi I'm trying to display certain data on my listview based on item selected from a spinner. This is how the display page currently looks. It's reading all the data from the node. Because "Running" is selected on the spinner above, I want the list view to only show Running.

Here is the code i'm using to read all the data.

 @Override
    protected void onStart() {
        super.onStart();

        //reading data to listview, every time its saved
        currentUserDB2.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                cardiosList.clear();
                for(DataSnapshot cardioHistorySnapshot : dataSnapshot.getChildren()){

                    Cardio cardio = cardioHistorySnapshot.getValue(Cardio.class);

                    cardiosList.add(cardio);
                }

                CardioHistoryList adapter = new CardioHistoryList(Cardio_History.this, cardiosList);
                ListViewCardioHistory.setAdapter(adapter);

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

Working code

spinnerCardioHistory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    String name = spinnerCardioHistory.getSelectedItem().toString();


                    currentUserDB2.orderByChild("detailCategory").equalTo(name).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            cardiosList.clear();
                            for (DataSnapshot cardioHistorySnapshot : dataSnapshot.getChildren()){
                                Cardio cardio = cardioHistorySnapshot.getValue(Cardio.class);

                                cardiosList.add(cardio);
                            }
                            CardioHistoryList adapter = new CardioHistoryList(Cardio_History.this, cardiosList);
                            ListViewCardioHistory.setAdapter(adapter);
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });
                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }
            });
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134

1 Answers1

1

If you want only the word Running to be displayed then do the following:

 String text = spinner.getSelectedItem().toString();
 FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
 DatabaseReference currentUserDB2=FirebaseDatabase.getInstance().getReference().child("Cardio").child(user.getUid());
 currentUserDB2.orderByChild("detailCategory").equalTo(text).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            cardiosList.clear();
            for(DataSnapshot cardioHistorySnapshot : dataSnapshot.getChildren()){

               String category=cardioHistorySnapshot.child("detailCategory").getValue().toString();
                cardiosList.add(category);
            }

            CardioHistoryList adapter = new CardioHistoryList(Cardio_History.this, cardiosList);
            ListViewCardioHistory.setAdapter(adapter);

First you get the select texted that is in the spinner String text = spinner.getSelectedItem().toString();

then you add it in the query orderByChild("detailCategory").equalTo(text) and then retrieve from the database and it will only add the word that is selected in the spinner which is "Running".

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • i used the spinner on selected item and added the reading code there instead, I have the working code above. – Mohan Adhikari Mar 11 '18 at 15:50
  • `String name = spinnerCardioHistory.getSelectedItem().toString();` this can work even if it was not in itemselectedlistener, refer to this: https://stackoverflow.com/a/5787902/7015400 – Peter Haddad Mar 11 '18 at 15:52