目前我将2D Enum数组转换为2D int数组,然后将其传递给Bundle,并在另一端将其转换为1D Object数组,然后是2D int数组,最后返回到我的2D Enum数组。
有没有更好的方法来做到这一点?
我试图直接传递和获取二维数组枚举后,但我得到一个RuntimeException当我尝试找回它。
这里是我的代码:
传递二维数组的软件包:
// Send the correct answer for shape arrangement
Intent intent = new Intent(getApplicationContext(), RecallScreen.class);
Bundle bundle = new Bundle();
// Convert mCorrectShapesArrangement (Shapes[][]) to an int[][].
int[][] correctShapesArrangementAsInts = new int[mCorrectShapesArrangement.length][mCorrectShapesArrangement[0].length];
for (int i = 0; i < mCorrectShapesArrangement.length; ++i)
for (int j = 0; j < mCorrectShapesArrangement[0].length; ++j)
correctShapesArrangementAsInts[i][j] = mCorrectShapesArrangement[i][j].ordinal();
// Pass int[] and int[][] to bundle.
bundle.putSerializable("correctArrangement", correctShapesArrangementAsInts);
intent.putExtras(bundle);
startActivityForResult(intent, RECALL_SCREEN_RESULT_CODE);
检索出束:
Bundle bundle = getIntent().getExtras();
// Get the int[][] that stores mCorrectShapesArrangement (Shapes[][]).
Object[] tempArr = (Object[]) bundle.getSerializable("correctArrangement");
int[][] correctShapesArrangementAsInts = new int[tempArr.length][tempArr.length];
for (int i = 0; i < tempArr.length; ++i)
{
int[] row = (int[]) tempArr[i];
for (int j = 0; j < row.length; ++j)
correctShapesArrangementAsInts[i][j] = row[j];
}
// Convert both back to Shapes[][].
mCorrectShapesArrangement = new Shapes[correctShapesArrangementAsInts.length][correctShapesArrangementAsInts[0].length];
for (int i = 0; i < correctShapesArrangementAsInts.length; ++i)
for (int j = 0; j < correctShapesArrangementAsInts[0].length; ++j)
mCorrectShapesArrangement[i][j] = Shapes.values()[correctShapesArrangementAsInts[i][j]];
在此先感谢!
2012-06-06
Felix