Home > database >  Android Studio: Unable to print cursor results
Android Studio: Unable to print cursor results

Time:06-12

I am attempting to print the cursor results to log.d.

When I print the results of the cursor, it does not print the array.

i.e. D/Row values: com.example.androidlabs.Todo@7c9655f

Here is the code:

  public void printCursor(Cursor c) {
        //The database version number using db.getVersion for the version number.
        int version = db.getVersion();

        //The number of rows in the cursor
        int rowCount = c.getCount();

        //The number of columns in the cursor
        int columnCount = c.getColumnCount();

        //The names of the columns in the cursor
        String[] columnNames = c.getColumnNames();

        //The results of each row in the cursor
        ArrayList<Todo> rowValuesList = new ArrayList<>();

        int ColIndex = c.getColumnIndex(myOpener.COL_1);
        int itemColIndex = c.getColumnIndex(myOpener.COL_2);
        int urgentColIndex = c.getColumnIndex(myOpener.COL_3);

        c.moveToFirst();

        if(c.moveToFirst()) {

                long id = c.getLong(ColIndex);
                String item = c.getString(itemColIndex);
                int urgentInt = c.getInt(urgentColIndex);

                if (urgentInt == 1) {
                    urgent = true;
                } else {
                    urgent = false;
                }

                rowValuesList.add(new Todo(item, urgent, id));
        }

        String rowValues = TextUtils.join(",", rowValuesList);

        //Printing variables to log
        Log.d("Database version", String.valueOf(version));
        Log.d("Row count", String.valueOf(rowCount));
        Log.d("Column count", String.valueOf(columnCount));
        Log.d("Column names", Arrays.toString(columnNames));
        Log.d("Row values", rowValues);

    }

Other options I have tried that have not worked:

Log.d("Row values", rowValuesList.toString());
 for (Todo t : rowValuesList) {
            Log.d("Row values", String.valueOf(t));
        }
StringBuilder s = new StringBuilder();
        for(Todo todo : rowValuesList) {
            s.append(todo);
            s.append(",");
        }
        Log.d("Row values", String.valueOf(s));

I know the cursor is not empty as it is displays the results when loaded from SQLite on the application.

Any advice would be helpful.

Thank you,

CodePudding user response:

Your output is:

D/Row values: com.example.androidlabs.Todo@7c9655f

That would make sense if:

  • rowValuesList contains a single Todo object, and
  • Your Todo class does not have a custom implementation of toString()

The default implementation of toString() that you inherit from Object gives a result like what you see: the fully qualified class name and an object ID, separated by @.

  • Related