Using MergeCursor and SimpleCursorAdapter, What Missing?

No matter what I do, the following throws an error that one of the columns contained in cursor_counterparty does not exist. When I checked merge_cursor, I can find the column there, here is my code, what am I doing wrong?

cursor_invoices = Invoices.getInvoicesCursor(counterparty.getId()); Cursor cursor_counterparty = Counterparties .getCounterpartyCursor(counterparty.getId()); startManagingCursor(cursor_invoices); startManagingCursor(cursor_counterparty); /* Joins cursors akin to doing an SQL join */ MergeCursor merge_cursor = new MergeCursor(new Cursor[] { cursor_invoices, cursor_counterparty }); merge_cursor.moveToFirst(); int[] listview_columns = new int[] { R.id.textview_invoice_number, R.id.textview_counterparty_name, R.id.textview_amount, R.id.textview_account_name, R.id.textview_invoice_date, R.id.textview_date_paid }; String[] listview_fields = new String[] { App.INVOICENUMBER, App.COUNTERPARTYNAME, counterparty_amount_field, App.ACCOUNTNAME, App.INVOICEDATE, App.DATEPAID }; SimpleCursorAdapter cursor_adapter_invoices = new SimpleCursorAdapter( this, R.layout.listview_invoice_item, merge_cursor, listview_fields, listview_columns); 

The error I am getting is:

java.lang.IllegalArgumentException: column 'counterparty_name' does not exist

When I debug the application, I see "counterparty_name" as a column in one of the cursors in merge_cursor.

Any help would be great, thanks!

+4
source share
1 answer

Do you want to join the cursors vertically (by adding rows) or horizontally (by adding columns)?

This is a theory since I did not look into the code, but it makes sense to me ...

MergeCursor combines cursors vertically (fact) one by one. So, for the part of the cursor, you have one set of columns, and for the other you have a different set of columns (assumption).

Your adapter is trying to find a column that does not exist in one part or another for the displayed row.

If you changed to CursorJoiner, which concatenates the columns, I think it will work more as you expect, although no matter how you match the rows, I don't know.

Good explanation here

EDIT

I see that you use the same identifier to enter each cursor, so my concern about building them does not matter. I think you need CursorJoiner, not MergeCursor.

+4
source

Source: https://habr.com/ru/post/1412221/


All Articles