如何在Android中使用游标数据创建列表数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1354006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:58:55  来源:igfitidea点击:

How can I create a list Array with the cursor data in Android

androidarraylistandroid-sqliteandroid-cursor

提问by Dennie

How can I create a list Array (the list display First Alphabet when scroll) with the cursor data?

如何使用光标数据创建列表数组(滚动时列表显示第一个字母)?

回答by Isaac Waller

Go through every element in the Cursor, and add them one by one to the ArrayList.

遍历 中的每个元素Cursor,并将它们一一添加到ArrayList.

ArrayList<WhateverTypeYouWant> mArrayList = new ArrayList<WhateverTypeYouWant>();
for(mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()) {
    // The Cursor is now set to the right position
    mArrayList.add(mCursor.getWhateverTypeYouWant(WHATEVER_COLUMN_INDEX_YOU_WANT));
}

(replace WhateverTypeYouWantwith whatever type you want to make a ArrayListof, and WHATEVER_COLUMN_INDEX_YOU_WANTwith the column index of the value you want to get from the cursor.)

(替换WhateverTypeYouWant为您想要创建的任何类型ArrayList,以及WHATEVER_COLUMN_INDEX_YOU_WANT您想要从游标中获取的值的列索引。)

回答by imbrizi

One quick correction: the for loop above skips the first element of the cursor. To include the first element, use this:

一个快速更正:上面的 for 循环跳过光标的第一个元素。要包含第一个元素,请使用:

ArrayList<String> mArrayList = new ArrayList<String>();
mCursor.moveToFirst();
while(!mCursor.isAfterLast()) {
     mArrayList.add(mCursor.getString(mCursor.getColumnIndex(dbAdapter.KEY_NAME))); //add the item
     mCursor.moveToNext();
}

回答by PearsonArtPhoto

Even better than @imbrizi's answer is this:

比@imbrizi 的回答更好的是:

ArrayList<String> mArrayList = new ArrayList<String>();
while(mCursor.moveToNext()) {
     mArrayList.add(mCursor.getString(mCursor.getColumnIndex(dbAdapter.KEY_NAME))); //add the item
}

moveToNext()will return false if there isn't anything left, so this reduces the SLOC by a few, and is easier to see.

moveToNext()如果没有任何东西,将返回 false,因此这将 SLOC 减少了一些,并且更容易看到。

Even better is to get the column index outside of the loop.

更好的是在循环之外获取列索引。

ArrayList<String> mArrayList = new ArrayList<String>();
int columnIndex=mCursor.getColumnIndex(dbAdapter.KEY_NAME)
while(mCursor.moveToNext()) {
     mArrayList.add(mCursor.getString(columnIndex)); //add the item
}

回答by Micro

This one worked really well for me because I wanted an arraylist of objects:

这个对我来说非常有效,因为我想要一个对象数组:

List<MyObject> myList = new ArrayList<String>();
Cursor c = ...

while(c.moveToNext()) {
    myList.add(new MyObject(cursor.getInt(cursor.getColumnIndex("_id")), cursor.getString(cursor.getColumnIndex("column1")), cursor.getInt(cursor.getColumnIndex("column2")));        
}
c.close();

Just make a POJO MyObjectand make sure it has a constructor.

只需制作一个 POJOMyObject并确保它有一个构造函数。

回答by Rafols

In Kotlin you can use this extension:

在 Kotlin 中你可以使用这个扩展:

fun <T> Cursor.toList(block: (Cursor) -> T) : List<T> {
    return mutableListOf<T>().also { list ->
        if (moveToFirst()) {
            do {
                list.add(block.invoke(this))
            } while (moveToNext())
        }
    }
}

and use it:

并使用它:

val listOfIds = cursor.toList { 
    // create item from cursor. For example get id:
    it.getLong(it.getColumnIndex("_id"))
}