简体   繁体   中英

Android Firebase - Change listview data from firebase based on spinner

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) { } });

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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM