1

I have a problem with Firebase database.

When I get data from database, I get old data on the top. How I can change it to get new data on the top?

in firebase console new data also are at the bottom

I'm using listView for FirebaseAdapter.

  // setting adapter
             private void showAdapter() { 
                    fBListAdapter = new FirebaseListAdapter<DiscountProduct>(ListDiscountActivity.this, DiscountProduct.class, R.layout.model_of_product, refDiscount) {
                        @Override
                        protected void populateView(View v, DiscountProduct model, int position) {
                            TextView nameProduct;

                            nameProduct = (TextView) .findViewById(R.id.modelCardNameProduct);

                            nameProduct.setText(model.getNameProduct());
                        }

                    };

                    listofDiscountProducts.setAdapter(fBListAdapter); //It's ListView on Activity
                }

it void for adding new data in firebase database

 //adding data to firebase database
     public static void createNewDiscountPost(String nameProduct) {
            String key = refDiscount.push().getKey();
            DiscountPost discountPost = new DiscountPost(nameProduct);

            Map<String, Object> createNewDiscountPost = discountPost.toMap();

            Map<String, Object> childUpdates = new HashMap<>();
            childUpdates.put("/" + key, createNewDiscountPost);

            refDiscount.updateChildren(childUpdates);
        }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Also see http://stackoverflow.com/questions/34156996/firebase-data-desc-sorting-in-android – Frank van Puffelen Apr 27 '17 at 13:19
  • To fetch the newest records with push ids in a query (as some coming to this thread will be trying to do) use `limitToLast(n)`, which will give you items from the end of the list. – Kato Apr 27 '17 at 18:11

1 Answers1

4

make getitem inside your listview adapter return your items in reverse order

@Override
public Object getItem(int position) {
   return super.getItem(super.getCount() - position - 1);
}

or you can try listView.setStackFromBottom(true);

for more information about how data is ordered go to officaial docs here and this link too

Mohamed Nageh
  • 1,963
  • 1
  • 19
  • 27