Serialization issue with SortedSet, Arrays, an Serializable

爷,独闯天下 提交于 2019-11-29 17:13:59

Finally I got it to work this way:

Serializable s = this.getIntent().getSerializableExtra("results");
Object[] o = (Object[]) s;
if (o != null) {
    resultSet = new TreeSet<RatedMessage>(new Comp());
    for (int i = 0; i < o.length; i++) {
        if (o[i] instanceof RatedMessage) {
            resultSet.add((RatedMessage) o[i]);
        }
    }
}

I'm sorry; I overlooked the use of the no-arg toArray() call.

Please note that there's overloaded toArray(T[]) method that takes an array as an argument.

By using this form, you can control the component type of the array, and it will work as expected.

protected void onPostExecute(SortedSet<RatedMessage> result) {
  lancon.putExtra("results", result.toArray(new RatedMessage[result.size()]));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!