Android cursor.moveToNext ()?

I am trying to query all columns in a table into one long textual view and / or row. I know this may be the wrong way, but I have to do it. Correct me, if I'm wrong, I got the impression that the next step will get the next column in the row:

Cursor c = db.get(); if(c.moveToFirst){ do{ string = c.getString(0); }while(c.moveToNext); 

I thought this would get the first column and display all its contents, instead I would get the first column and the first row. What am I doing wrong? Is there a better or real way to get this information without using a ListView?

+7
source share
5 answers

For clarity, a complete example would be the following, which I hope is of interest. As stated in the code comments, we essentially iterate over the rows of the database and then the columns to form the data table according to the 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(); } 
+9
source

Simple use:

 Cursor cursor = db.query(...); while (cursor.moveToNext()) { ... } 

moveToFirst is used when you need to start an iteration from the beginning after you have already reached a position.

Avoid using cursor.getCount () unless necessary. And never use the getCount () loop.

getCount is expensive - it iterates through many records to count them. It does not return a stored variable. There may be some caching on the second call, but the first call does not know the answer until it is counted.

If your query matches 1000 lines, the cursor actually only has the first line. Each moveToNext performs a search and finds the next match. getCount should find all 1000. Why iterate over everything if you need only 10? Why repeat twice?

In addition, if your query does not use an index, getCount can be even slower - getCount can go through 10,000 records, even if the query matches only 100. Why is the cycle 20,000 instead of 10,000?

+30
source

moveToNext move the cursor to the next line. and c.getString (0) will always give you the first column, if any. I think you should do something like this inside the loop

 int index = c.getColumnIndex("Column_Name"); string = c.getString(index); 
+1
source

cursor.moveToFirst() moves the cursor to the first line. If you know that you have 6 columns and you want one row to contain all 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(); 
+1
source

I encode my loops over cusror as follows:

  cursor.moveToFirst(); while(!cursor.isAfterLast()) { cursor.getString(cursor.getColumnIndex("column_name")); cursor.moveToNext(); } 

It always works. This will return the column_name column values โ€‹โ€‹of all rows. Your mistake is that you iterate over rows, not columns. To iterate over columns:

 cursor.moveToFirst(); for(int i = 0; i < cursor.getColumnNames().length; i++){ cursor.getString(i); } 

This will loop around the columns of the first row and get each column value.

0
source

All Articles