In my application I use cursor to get information from SQLite data base like this:
Cursor contacts = dataBase.select("SELECT _idContact FROM Contacts");
if (contacts.getCount() > 0) {
if (IMLayout.getVisibility() == View.VISIBLE) {
int k =开发者_如何学运维 contacts.getCount();
for (int j = 0; j < k; j++) {
if (j == 0) {
contacts.moveToFirst();
} else {
contacts.moveToNext();
}
What I want is to optimize the "for" using Enhanced for loop. For that I have to use an array, or other, but not cursors because the cursors are not working for Enhanced for loop. How to convert the cursor into an arrayList?
The enhanced for-loop only works on collections, iterables and arrays, and Cursor
is none of those things.
You'll just have to use the methods provided, as you currently do. Alternatively, you'll have to suck the contents of the cursor into a collection, but then you're just making more work for yourself.
You can greatly simplify this logic as follows:
Cursor contacts = dataBase.select("SELECT _idContact FROM Contacts");
while (contacts.moveToNext()) {
// do stuff with this database entry
}
精彩评论