1

I have a firebase database that has a name and number in it for each of my stores. I want a spinner to show the names of each store so we can select one. the store name example is "0023 franklin", and i want to order them by the number.

my code to make the spinner is

    mydb.orderByValue().addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Is better to use a List, because you don't know the size
            // of the iterator returned by dataSnapshot.getChildren() to
            // initialize the array
            
            final List<String> areas = new ArrayList<String>();

            for (DataSnapshot areaSnapshot: dataSnapshot.getChildren()) {
                Store storeName = areaSnapshot.getValue(Store.class);
                areas.add(storeName.getStoreName());
            }

            Spinner areaSpinner = (Spinner) findViewById(R.id.spinner);
            ArrayAdapter<String> areasAdapter = new ArrayAdapter<String>(checkout.this, android.R.layout.simple_spinner_item, areas);
            areasAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            areaSpinner.setAdapter(areasAdapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    }); 

it shows all the stores but doesn't order them by the number. and the stores are not added in the database in order.

how to i make this happen?

adding JSON

storelist:

-KvIoZC0AbpD1-SJJrIS

faxNum: 987654321

storeName: 0024 Franklin

-KvIobgHLouocLpMkN6k

faxNum: 1234567890

storeName: 0003 Randle

Community
  • 1
  • 1
  • Can you edit your question to show a snippet of the JSON you're trying to display? You can get this by clicking the "Export JSON" link in your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Sep 30 '17 at 17:54

1 Answers1

0

You can use any of the following methods to sort the data:

orderByChild()

orderByValue()

orderByKey()

You can store that number with a key like 'storeNumber' and use the available method like:

orderByChild("storeNumber");

for more usage refer to : https://firebase.google.com/docs/database/android/lists-of-data?authuser=0#sort_data

Community
  • 1
  • 1
hittsss
  • 76
  • 5