Using addrow for MatrixCursor, how to add different object types?

5k views Asked by At

I am trying to input data from a List<ParseObject> to a cursor. ParseObject documentation can be found here.

I found out that you can use MatrixCursor to do this, referring to helpful sites: http://developer.android.com/reference/android/database/MatrixCursor.html

How to insert extra elements into a SimpleCursorAdapter or Cursor for a Spinner?

How to create Cursor data without getting data from the DataBase in Android application?

My problem is, when I return the cursor from MyContentProvider.java, and I try to do a cursor.getFloat(), it returns a String and I cannot do DecimalFormat on it.

Question:

How do I put more than one kind of data type into the MatrixCursor using, addRow (Object[] columnValues)?

Method that uses Cursor data:

public void getData() {
    Cursor cursor = getContentResolver().query(URI, null, mParseId, null, null);

    DecimalFormat df = new DecimalFormat();
    df.setMinimumFractionDigits(2);

    mTextview.setText(df.format(
        cursor.getFloat(cursor.getColumnIndexOrThrow("number"))));
}

MyContentProvider.java:

@Override
public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder) {
  ......
  MatrixCursor mc = new MatrixCursor(new String[] { "_id", "title", "number" });

  mc.addRow(new String[] { ParseObject.getString("id"), ParseObject.getString("title"), ParseObject.getString("number") });
  return mc;
}

What I want to do in MyContentProvider.java:

@Override
public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder) {
  ......
  MatrixCursor mc = new MatrixCursor(new String[] { "_id", "title", "number" });

  mc.addRow(new String[] { ParseObject.getLong("id"), ParseObject.getString("title"), ParseObject.getDouble("number") });
  return mc;
}
1

There are 1 answers

0
Mikolasan On

I'm not sure that I understand your code samples, but I can answer on your main question

How do I put more than one kind of data type into the MatrixCursor using, addRow (Object[] columnValues)?

Change array type from String to Object:

mc.addRow(new Object[] {1, "text", 0.666});