Created
June 22, 2017 11:53
-
-
Save SerggioC/5e3813f549e5066b02e0cfcf0b6d5735 to your computer and use it in GitHub Desktop.
Cursor in a loop?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DBHelper dbHelper = new DBHelper(getContext()); | |
SQLiteDatabase db = dbHelper.getWritableDatabase(); | |
List<String> distinct_id_products = new ArrayList<>(); | |
ArrayList<ArrayList<Double>> priceValues_Array_arrayList = new ArrayList<>(); | |
String query1 = "SELECT DISTINCT " + ProductsContract.PricesEntry.COLUMN_ID_PRODUCTS + | |
" FROM " + ProductsContract.PricesEntry.TABLE_NAME + | |
" ORDER BY " + ProductsContract.PricesEntry.COLUMN_ID_PRODUCTS; | |
Cursor cursor1 = db.rawQuery(query1, null); | |
int cursor1Count = cursor1.getCount(); | |
if (cursor1Count > 0) { | |
while (cursor1.moveToNext()) { | |
String _id_poducts = cursor1.getString(cursor1.getColumnIndex(ProductsContract.PricesEntry.COLUMN_ID_PRODUCTS)); | |
distinct_id_products.add(_id_poducts); | |
} | |
priceValues_Array_arrayList = new ArrayList<>(cursor1Count); | |
} else { | |
// TODO Empty database! | |
} | |
cursor1.close(); | |
for (int i = 0; i < distinct_id_products.size(); i++) { | |
Cursor cursor2 = db.rawQuery("SELECT * FROM " + ProductsContract.PricesEntry.TABLE_NAME + | |
" WHERE " + ProductsContract.PricesEntry.COLUMN_ID_PRODUCTS + " = '" + distinct_id_products.get(i) + "'" + | |
" ORDER BY " + ProductsContract.PricesEntry.COLUMN_PRODUCT_PRICE_DATE + " ASC", null); | |
int cursor2Count = cursor2.getCount(); | |
if (cursor2Count > 0) { | |
ArrayList<Double> priceValues_arrayList = new ArrayList<>(cursor2Count); | |
while (cursor2.moveToNext()) { | |
double price_value = cursor2.getDouble(cursor2.getColumnIndex(ProductsContract.PricesEntry.COLUMN_PRODUCT_PRICE_VALUE)); | |
priceValues_arrayList.add(price_value); | |
} | |
priceValues_Array_arrayList.add(priceValues_arrayList); | |
} else { | |
// TODO Empty database! | |
} | |
cursor2.close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment