Android Firebase Sorting

Dataless :

I am building an app. I have a Firebase database with a bunch of products, each of them has a rating. I am displaying them in a ListView but want to sort them by their rating. Do I need to save them differently to get this to work ?

  private ListView mTopRatedList;
    ArrayList<String> mAllBeers = new ArrayList<>();
    DatabaseReference Reference = FirebaseDatabase.getInstance().getReference().child("Beers");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_top_rated);

        mTopRatedList =(ListView)findViewById(R.id.topRatedList);
        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mAllBeers);
        mTopRatedList.setAdapter(arrayAdapter);
        arrayAdapter.clear();


        Reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                arrayAdapter.clear();
                for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                    //arrayAdapter.add(snapshot.getValue().toString());
                    Beers beers = snapshot.getValue(Beers.class);
                    String beerClass = beers.getmName();
                    Float beerRating = beers.getmRating();
                    //Collections.sort()
                    arrayAdapter.add(beerClass+ "User Rating is " + beerRating);

                }
                arrayAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });


    }
}

Frank van Puffelen :

From the look of your code, each of your beers has a rating property in the database.

In that case you can show the beers ordered by rating by doing:

Reference.orderByChild("rating").addValueEventListener(new ValueEventListener() {
    ...

A few notes:

  • This will show the beers in ascending order of their rating, so with the lowest value first. If you want to show the highest rated beer first, Firebase does not have a built-in operator for descending sort. So you'll have to work around that by either reversing the results in your client-side code, or by including an inverted rating in the database too for this purpose. In the latter case you'd sort on the inverted rating, and then display the regular rating.
  • For this to work correctly it is important that the rating value is a number, and not stored as a string. When stored as a string it may work for values up to 9, but after that you'll run into cases where numerical ordering differs from the lexicographical ordering that Firebase will use for strings.
  • Please never leave onCancelled empty as it hides potential problems. Its minimal implementation is public void onCancelled(@NonNull DatabaseError databaseError) { throw databaseError.toException(); }.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=352824&siteId=1