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

发布于 2024-08-03 04:16:24 字数 36 浏览 9 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

七婞 2024-08-10 04:16:24

遍历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));
}

(将 WhateverTypeYouWant 替换为您想要创建 ArrayList 的任何类型,并将 WHATEVER_COLUMN_INDEX_YOU_WANT 替换为您想要从光标。)

Go through every element in the Cursor, and add them one by one to the 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 WhateverTypeYouWant with whatever type you want to make a ArrayList of, and WHATEVER_COLUMN_INDEX_YOU_WANT with the column index of the value you want to get from the cursor.)

我只土不豪 2024-08-10 04:16:24

一个快速更正:上面的 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();
}

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

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();
}
风吹短裙飘 2024-08-10 04:16:24

比 @imbrizi 的答案更好的是:

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

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

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

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

Even better than @imbrizi's answer is this:

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.

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
}
蓝色星空 2024-08-10 04:16:24

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

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();

只需创建一个 POJO MyObject 并确保它有一个构造函数。

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 MyObject and make sure it has a constructor.

娇妻 2024-08-10 04:16:24

在 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())
        }
    }
}

并使用它:

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

In Kotlin you can use this extension:

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"))
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文