【问题标题】:Android: How to pass 2D Enum array to a Bundle?Android:如何将 2D Enum 数组传递给 Bundle?
【发布时间】:2012-06-10 02:49:04
【问题描述】:

我有一个 2D 枚举数组,我想在我的两个活动之间传递。

目前我将 2D Enum 数组转换为 2D int 数组,将其传递给 Bundle,然后将其转换为 1D Object 数组,然后转换为 2D int 数组,最后返回到我的 2D Enum 数组。

有没有更好的方法来做到这一点?

签出Android: How to put an Enum in a Bundle?后,我通过单个枚举没有问题

我尝试直接传递和检索 2D Enum 数组,但是当我尝试检索它时出现 RuntimeException。

这是我的代码:

将二维数组传递给 Bundle:

    // 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 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]];

提前致谢!

【问题讨论】:

    标签: android arrays enums 2d bundle


    【解决方案1】:

    在实现Parcelable 的 Enum(getter/setter) 上创建一个包含您的二维数组的自定义类,然后您可以通过意图传递该自定义类的对象。

    class customclass implements Parcelable { 
       public enum Foo { BAR, BAZ }
    
       public Foo fooValue;
    
       public void writeToParcel(Parcel dest, int flags) {
          dest.writeString(fooValue == null ? null : fooValue.name());
       }
    
       public static final Creator<customclass> CREATOR = new Creator<customclass>() {
         public customclass createFromParcel(Parcel source) {        
           customclass e = new customclass();
           String s = source.readString(); 
           if (s != null) e.fooValue = Foo.valueOf(s);
           return e;
         }
       }
     }
    

    【讨论】:

    • 太棒了,感谢您的快速响应!现在让我试着处理一下。
    • 所以我的自定义类包含整个二维数组,而不仅仅是一个枚举?
    • writeToParcel 中的“parcel”字段是否意味着“dest”?
    • 对上面的评论也是这样吗:)
    猜你喜欢
    • 2012-10-22
    • 2021-04-21
    • 2022-01-14
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    相关资源
    最近更新 更多