1

I would like to print the following information to the logcat window in Android:

• The database version number (using db.getVersion() for the version number). • The number of columns in the cursor. • The name of the columns in the cursor. • The number of results in the cursor • Each row of results in the cursor

I have created the following class at the end of my app to accomplished this:

public void printCursor(Cursor c, int version) {

    int dbVersion = version;
    int numberCursorColumns = c.getColumnCount();
    String[] nameCursorColumns = c.getColumnNames();
    int numberCursorResults = c.getCount();
    List<Message> cursorRowValuesList = new ArrayList<>();
    String cursorRowValues;

    c.moveToFirst();

    if (c.moveToFirst()) {

        int sendColIndex = c.getColumnIndex(MyOpener.COL_TEXT);
        int typeColIndex = c.getColumnIndex(MyOpener.MESSAGE_TYPE);
        int idColIndex = c.getColumnIndex(MyOpener.COL_ID);

        String send = c.getString(sendColIndex);
        int type = c.getInt(typeColIndex);
        long id = c.getLong(idColIndex);

        cursorRowValuesList.add(new Message(send, type, id));
    }
        cursorRowValues = TextUtils.join(",", cursorRowValuesList);


    Log.i("DATABASE VERSION", Integer.toString(dbVersion));
    Log.i("NUMBER OF COLUMNS", Integer.toString(numberCursorColumns));
    Log.i("COLUMN NAMES", Arrays.toString(nameCursorColumns));
    Log.i("NUMBER OF RESULTS", Integer.toString(numberCursorResults));
    Log.i("ROW VALUES", cursorRowValues);
}

All of the printouts work, except for the final one, ROW VALUES, which is supposed to print each row of results in the cursor. This is what prints in the logcat when I run the app:

02-14 14:52:35.222 5969-5969/com.example.androidlabs I/DATABASE VERSION: 1

02-14 14:52:35.222 5969-5969/com.example.androidlabs I/NUMBER OF COLUMNS: 3

02-14 14:52:35.222 5969-5969/com.example.androidlabs I/COLUMN NAMES: [_id, TEXT, TYPE]

02-14 14:52:35.222 5969-5969/com.example.androidlabs I/NUMBER OF RESULTS: 3

02-14 14:52:35.222 5969-5969/com.example.androidlabs I/ROW VALUES:com.example.androidlabs.Message@55b3428

Why am I getting that message for ROW VALUES?

DrVonSamuel
  • 41
  • 1
  • 5

1 Answers1

0

Why am I getting that message for ROW VALUES?

cursorRowValues is created using:

cursorRowValues = TextUtils.join(",", cursorRowValuesList);

cursorRowValuesList is created through your loop, with a new Message object being added on each pass of the loop:

cursorRowValuesList.add(new Message(send, type, id));

So, your cursorRowValues reflects that cursorRowValuesList holds a single Message object. You did not override toString() on Message, so you get the default implementation.

You might want to override toString() on Message, if you want some other output.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks. Any suggestions on how to override toString() on Message? – DrVonSamuel Feb 14 '21 at 23:31
  • @DrVonSamuel: Um, add a `toString()` method. You can learn more about `toString()` in any decent book on Java. Or, you can look at [this](https://stackoverflow.com/q/3546362/115145), [this](https://stackoverflow.com/q/10734106/115145), or [this](https://stackoverflow.com/q/53974078/115145). – CommonsWare Feb 14 '21 at 23:36
  • I am trying to convert ArrayList to ArrayList so that I can then run toString(). Still though, that doesn't work: List stringValues = new ArrayList(cursorRowValuesList.size()); for (Message messageObject : cursorRowValuesList) { stringValues.add(messageObject != null ? messageObject.toString() : null); } cursorRowValues = stringValues.toString(); – DrVonSamuel Feb 15 '21 at 00:09