androidlistviewcursorlistview-adaptermatrixcursor

Android Cursor odd and even rows into MatrixCursor


Hee guys,

I got 2 ListViews, one for the left side and one for the right side. However I have one Cursor with values. Now I want to get the even rows from the cursor and put that in a matrixcursor to fill the left ListView and with the odds I want to fill the right side.

However i got like 9 columns, is there anyway to just put the whole row into a matrixCursor without selecting each column by itself? So that when something changes about the db or anything I dont have to manually adjust the columns.

Maybe I could just use a Cursor instead of a MatrixCursor, which would be better?


Solution

  • Solution:

    MatrixCursor lCursor;
    MatrixCursor rCursor;
    String[] columnNames = new String[mCursor.getColumnCount()];
    mCursor.moveToFirst();
    for (int j = 0; j < mCursor.getColumnCount(); j++) {
        columnNames[j] = mCursor.getColumnName(j);
    }
    lCursor = new MatrixCursor(columnNames);
    rCursor = new MatrixCursor(columnNames);
    
    if (mCursor.getCount() > 0) {
        for (int i = 0; i < mCursor.getCount(); i++) {
            mCursor.moveToPosition(i);
            if ((i & 1) == 0) {
                String[] values = new String[mCursor.getColumnCount()];
                for (int j = 0; j < mCursor.getColumnCount(); j++) {
                    values[j] = mCursor.getString(j);
                }
                lCursor.addRow(values);
            } else {
                String[] values = new String[mCursor.getColumnCount()];
                for (int j = 0; j < mCursor.getColumnCount(); j++) {
                    values[j] = mCursor.getString(j);
                }
                rCursor.addRow(values);
            }
        }
        left.setAdapter(new HomeMenuAdapter(MainActivity.getContext(),
                lCursor));
        right.setAdapter(new HomeMenuAdapter(MainActivity.getContext(),
                rCursor));
    }