2
從Android上的SQLite數據庫中提取數據有一個很奇怪的問題。這是我的方法,它從表entries
獲取所有條目,並在循環中迭代它們,將它們放入列表視圖中。SQLiteDatabase順序編號跳過第一個元素
它正常工作,直到我想這條線 Cursor c = readableDatabase.rawQuery("select * from entries", null);
改變這一個: Cursor c = readableDatabase.rawQuery("select * from entries order by ID DESC", null);
所以改變後,我沒有看到的第一個元素。不知道那裏發生了什麼。這是該方法的源代碼。
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);
任何幫助將不勝感激
remove moveToFirst()方法然後它工作正常 –