android cursor.moveToNext()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10081631/
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
android cursor.moveToNext()?
提问by Christian
I am trying to query all the columns in a table into one long text view and/or string. I know this might not be the right way to do things but I have to do this. Correct me if I am wrong, I was under the impression that move next would get the next column in the row:
我试图将表中的所有列查询到一个长文本视图和/或字符串中。我知道这可能不是正确的做事方式,但我必须这样做。如果我错了,请纠正我,我的印象是下一步将获得该行中的下一列:
Cursor c = db.get();
if(c.moveToFirst){
do{
string = c.getString(0);
}while(c.moveToNext);
I thought that this would get the first column and display all of its contents instead I get the first column and first row. What am I doing wrong? Is there a better or real way to get this information without using a ListView?
我认为这将获得第一列并显示其所有内容,而不是获得第一列和第一行。我究竟做错了什么?在不使用 ListView 的情况下,是否有更好或真实的方法来获取此信息?
回答by Pla
The simple use is:
简单的用法是:
Cursor cursor = db.query(...);
while (cursor.moveToNext()) {
...
}
moveToFirst is used when you need to start iterating from start after you have already reached some position.
当您到达某个位置后需要从头开始迭代时,使用 moveToFirst。
Avoid using cursor.getCount() except if it is required. And never use a loop over getCount().
除非需要,否则避免使用 cursor.getCount()。并且永远不要在 getCount() 上使用循环。
getCount is expensive - it iterates over many records to count them. It doesn't return a stored variable. There may be some caching on a second call, but the first call doesn't know the answer until it is counted.
getCount 很昂贵 - 它遍历许多记录来计算它们。它不返回存储的变量。第二个调用可能会有一些缓存,但第一个调用在计数之前不知道答案。
If your query matches 1000 rows, the cursor actually has only the first row. Each moveToNext searches and finds the next match. getCount must find all 1000. Why iterate over all if you only need 10? Why iterate twice?
如果您的查询匹配 1000 行,则游标实际上只有第一行。每个 moveToNext 搜索并找到下一个匹配项。getCount 必须找到所有 1000 个。如果只需要 10 个,为什么要遍历所有?为什么要迭代两次?
Also, if your query doesn't use an index, getCount may be even slower - getCount may go over 10000 records even though the query matches only 100. Why loop 20000 instead of 10000?
此外,如果您的查询不使用索引,getCount 可能会更慢 - 即使查询仅匹配 100 条记录,getCount 也可能超过 10000 条记录。为什么循环 20000 而不是 10000?
回答by user1720179
For clarity a complete example would be as follows which I trust is of interest. As code comments indicated we essentially iterate over database rows and then columns to form a table of data as per database.
为清楚起见,我相信我感兴趣的完整示例如下。正如代码注释所示,我们基本上遍历数据库行,然后遍历列以形成每个数据库的数据表。
Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null,
null);
//if the cursor isnt null we will essentially iterate over rows and then columns
//to form a table of data as per database.
if (cursor != null) {
//more to the first row
cursor.moveToFirst();
//iterate over rows
for (int i = 0; i < cursor.getCount(); i++) {
//iterate over the columns
for(int j = 0; j < cursor.getColumnNames().length; j++){
//append the column value to the string builder and delimit by a pipe symbol
stringBuilder.append(cursor.getString(j) + "|");
}
//add a new line carriage return
stringBuilder.append("\n");
//move to the next row
cursor.moveToNext();
}
//close the cursor
cursor.close();
}
回答by mrd
I am coding my loops over the cusror like this:
我正在像这样在 cusror 上编码我的循环:
cursor.moveToFirst();
while(!cursor.isAfterLast()) {
cursor.getString(cursor.getColumnIndex("column_name"));
cursor.moveToNext();
}
That always works. This will retrieve the values of column "column_name" of all rows. Your mistake is that you loop over the rows and not the columns. To loop over the columns:
那总是有效的。这将检索所有行的“column_name”列的值。你的错误是你遍历行而不是列。要循环列:
cursor.moveToFirst();
for(int i = 0; i < cursor.getColumnNames().length; i++){
cursor.getString(i);
}
That will loop over the columns of the first row and retrieve each columns value.
这将遍历第一行的列并检索每个列的值。
回答by CChi
moveToNext move the cursor to the next row. and c.getString(0) will always give you the first column if there is one. I think you should do something similar to this inside your loop
moveToNext 将光标移动到下一行。如果有第一列,c.getString(0) 将始终为您提供第一列。我认为你应该在循环中做类似的事情
int index = c.getColumnIndex("Column_Name");
string = c.getString(index);
回答by Raunak
cursor.moveToFirst()
moves the cursor to the first row. If you know that you have 6 columns, and you want one string containing all the columns, try the following.
cursor.moveToFirst()
将光标移动到第一行。如果您知道您有 6 列,并且想要一个包含所有列的字符串,请尝试以下操作。
c.moveToFirst();
StringBuilder stringBuilder = new StringBuilder();
for(int i = 0; i < 6; i++){
stringBuilder.append(c.getString(i));
}
// to return the string, you would do stringBuilder.toString();