android文件夹cusr,android cursor.moveToNext()?

博客讨论了在Android中使用SQLite数据库查询时遇到的问题。主要集中在如何正确遍历数据库表格的所有列并将其内容合并到一个字符串中。文章提供了多种方法,包括使用Cursor的moveToNext()和getString()方法,强调避免使用getCount()进行循环,并给出了优化的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

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?

回答1:

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

}

回答2:

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.

Avoid using cursor.getCount() except if it is required.

And never use a loop over 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.

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?

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?

回答3:

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

int index = c.getColumnIndex("Column_Name");

string = c.getString(index);

回答4:

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.

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

回答5:

I am coding my loops over the cusror like this:

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:

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值