5

In my application, I am trying to pass a serializable object through an intent to another activity. The intent is not entirely created by me, it is created and passed through a search suggestion.

In the content provider for the search suggestion, the object is created and placed in the SUGGEST_COLUMN_INTENT_EXTRA_DATA column of the MatrixCursor. However, when in the receiving activity I call getIntent().getSerializableExtra(SearchManager.EXTRA_DATA_KEY), the returned object is of type String and I cannot cast it into the original object class.

I tried making a parcelable wrapper for my object that calls out.writeSerializable(...) and use that instead but the same thing happened.

The string that is returned is like a generic Object toString(), i.e. com.foo.yak.MyAwesomeClass@4350058, so I'm assuming that toString() is being called somewhere where I have no control.

Hopefully I'm just missing something simple. Thanks for the help!

Edit: Some of my code

This is in the content provider that acts as the search authority:

//These are the search suggestion columns 
private static final String[] COLUMNS = {
    "_id",  // mandatory column
    SearchManager.SUGGEST_COLUMN_TEXT_1,
    SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA
};

//This places the serializable or parcelable object (and other info) into the search suggestion
private Cursor getSuggestions(String query, String[] projection) {
    List<Widget> widgets = WidgetLoader.getMatches(query);

    MatrixCursor cursor = new MatrixCursor(COLUMNS);
    for (Widget w : widgets) {
        cursor.addRow(new Object[] {
                           w.id
                           w.name
                           w.data //This is the MyAwesomeClass object I'm trying to pass
                           });
    }

    return cursor;
}

This is in the activity that receives the search suggestion:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Object extra = getIntent().getSerializableExtra(SearchManager.EXTRA_DATA_KEY); 
    //extra.getClass() returns String, when it should return MyAwesomeClass, so this next line throws a ClassCastException and causes a crash
    MyAwesomeClass mac = (MyAwesomeClass)extra;
    ...
 }
4

1 回答 1

6

阅读我对类似问题的回答。基本问题是MatrixCursor仅适用于基本类型,并且依赖于AbstractCursor填充CursorWindow以在进程之间传递数据。AbstractCursor通过调用Object#toString每一行数据字段来做到这一点。换句话说,您不能通过MatrixCursor.

于 2010-06-16T22:42:48.527 回答