简体   繁体   English

Android:使用什么代替光标

[英]Android:What to use instead of cursor

In my application I use cursor to get information from SQLite data base like this: 在我的应用程序中,我使用游标从SQLite数据库中获取信息,如下所示:

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循环优化“ for”。 For that I have to use an array, or other, but not cursors because the cursors are not working for Enhanced for loop. 为此,我必须使用数组或其他数组,但不能使用游标,因为游标不适用于增强型for循环。 How to convert the cursor into an arrayList? 如何将光标转换为arrayList?

The enhanced for-loop only works on collections, iterables and arrays, and Cursor is none of those things. 增强的for循环仅适用于集合,可迭代对象和数组,而Cursor并非如此。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM