dimanche 24 mai 2015

SQLiteDatabase Order By Id Skips First Element

Have a very strange issue with pulling data from SQLite Database on Android. Here is my method that takes all the entries from the table entries and iterates them through the loop, putting them in the list view.

It works fine until I wanted to change this line Cursor c = readableDatabase.rawQuery("select * from entries", null); to this one: Cursor c = readableDatabase.rawQuery("select * from entries order by ID DESC", null);

So after the change I don't see the first element. Not sure what is going there. Here is the source code of that method.

EntryHelper entryHelper = new EntryHelper(this);
        SQLiteDatabase readableDatabase = entryHelper.getReadableDatabase();

        // NOTE: needs to be changed to order by ID...
        // 
        Cursor c = readableDatabase.rawQuery("select * from entries", null);
        c.moveToFirst();

        ArrayList productsList = new ArrayList();
        ProductsArrayAdapter productsArrayAdapter = new ProductsArrayAdapter(this, productsList);

        while (c.moveToNext()) {
            Integer entryIdIndex       = c.getColumnIndexOrThrow(Schema.Entry.ID);
            Integer entryBarcodeIndex =  c.getColumnIndexOrThrow(Schema.Entry.COLUMN_NAME_BARCODE);
            Integer entryQuantityIndex = c.getColumnIndexOrThrow(Schema.Entry.COLUMN_NAME_QUANTITY);

            Integer entryId      = c.getInt(entryIdIndex);
            String entryBarcode  = c.getString(entryBarcodeIndex);
            String entryQuantity = c.getString(entryQuantityIndex);

            Product lineItemProduct = new Product(entryId, entryBarcode, entryQuantity);
            productsList.add(lineItemProduct);

            Toast toast = Toast.makeText(this, lineItemProduct.toString(), Toast.LENGTH_SHORT);
            toast.show();
        }
        productsListView = (ListView) findViewById(R.id.productsList);
        productsListView.setAdapter(productsArrayAdapter);

Any help will be appreciated

Aucun commentaire:

Enregistrer un commentaire