I'm currently writing an app that pulls an array of longs from a server via json, and then passes that list from one activity into another. The basic skeleton looks like this:
public void onResponse(Map result)
{
ArrayList handles= (ArrayList)result.get("fileHandles");
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("handles", handles);
}
So the first problem becomes evident, the only methods for putExtra are putIntegerArrayListExtra
, putStringArrayListExtra
, putCharSequenceArrayListExtra
, and putParcelableArrayListExtra
. Thinking Long would probably be parcelable, I was wrong it doesn't work (even if I use ArrayList<Long>
). Next I thought I'd just pass a long [], however I see no straight-forward conversion to go from ArrayList<Long>
to long [] that intent.putExtra
will accept. This was the solution I finally ended up with:
ArrayList handles= (ArrayList)result.get("fileHandles");
long [] handleArray = new long[handles.size()];
for (int i = 0; i < handles.size(); i++)
{
handleArray[i] = Long.parseLong(handles.get(i).toString());
}
Obviously this seemed a little bit ridiculous to me, but every other conversion I tried seemed to complain for one reason or another. I've thought about re-thinking my serialization to have the problem taken care of before I get to this point, but I find it odd that passing ArrayList<Long>
from activity to activity could be so difficult. Is there a more obvious solution I'm missing?