【问题标题】:Pass 2D array to another Activity将二维数组传递给另一个活动
【发布时间】:2012-08-31 11:54:07
【问题描述】:

如何将 2 个面额数组对象作为参数传递给另一个活动

如何在另一个活动中获取二维数组字符串值

   String [][]str;

    Intent l = new Intent(context,AgAppMenu.class);
                 l.putExtra("msg",str);
                 l.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
                 context.startActivity(l);



  another Activity class

   String[][] xmlRespone2;

    xmlRespone2 = getIntent().getExtras().getString("msg");

【问题讨论】:

    标签: android multidimensional-array parcelable


    【解决方案1】:

    您可以使用putSerializable. 数组是可序列化的。

    存储:

    bundle.putSerializable("list", selected_list); // 这里的 bundle 是 Bundle 对象。

    访问:

    String[][] passedString_list = (String[][]) bundle.getSerializable("list");
    

    示例

    Intent mIntent = new Intent(this, Example.class);
    Bundle mBundle = new Bundle();
    mBundle.putSerializable("list", selected_list);
    mIntent.putExtras(mBundle);
    

    【讨论】:

    • 当我在不同的类中访问这个对象时,bunvle 会出错
    • public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.agappmenu); mno = getIntent().getExtras().getString("mno"); pinno = getIntent().getExtras().getString("pinno"); String[][] xmlRespone2 = (String[][]) bundle.getSerializable("msg");
    • 捆绑在我访问此值的另一个活动中无法解析
    • @ChristinaAyers 。如果我的回答对你有用,请接受。
    • @ChiragRaval 嗨,我尝试按照您的步骤进行操作,但是当我到达 resultMatrix = (double[][]) bundle.getSerializable("resultMatrix_"); 时,出现了异常 java.lang.Object[] cannot be cast to double[][]
    【解决方案2】:

    这终于对我有用了:

    要开始一个新的活动(发送 String[][] 和 String):

    String[][] arrayToSend=new String[3][30];
    String stringToSend="Hello";
    Intent i = new Intent(this, NewActivity.class);
    
    i.putExtra("key_string",stringToSend);
    
    Bundle mBundle = new Bundle();
    mBundle.putSerializable("key_array_array",  arrayToSend);
    i.putExtras(mBundle);
    
    startActivity(i);
    

    在 NewActivity.onCreate 中访问:

    String sReceived=getIntent().getExtras().getString("key_string");
    
    String[][] arrayReceived=null;
    Object[] objectArray = (Object[]) getIntent().getExtras().getSerializable("key_array_array");
    if(objectArray!=null){
        arrayReceived = new String[objectArray.length][];
        for(int i=0;i<objectArray.length;i++){
            arrayReceived[i]=(String[]) objectArray[i];
        }
    }
    

    【讨论】:

      【解决方案3】:

      您可以定义一个自定义类,该类实现 Parcelable 并包含从 Parcel 读取和写入二维数组的逻辑。之后,将那个可包裹的物体放入Bundle 内进行运输。

      【讨论】:

        【解决方案4】:

        将 2darray 设置为 public static void。 让 current_class 成为我们创建二维数组的类 我们想将数据传递给 NewActivity

        Class<?> ourClass=Class.forName("com.example.testapp.NewActivity");
        Intent ourIntent= new Intent(current_class.this,ourClass);
        intent_name.putExtra("name", 2darray_name);
        startActivity(ourIntent);`
        

        要在 NewActivity 中访问它,请使用 current_class.2darray_name 其中 current_class 是它最初定义的类。

        【讨论】:

          【解决方案5】:

          一个解决方案是您可以将其设置为静态,以便您可以在任何活动中使用它。

          Class A{
           public static String [][]str;
          ...
              Intent l = new Intent(context,AgAppMenu.class);
                           l.putExtra("msg",str);
                           l.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
                           context.startActivity(l);
          
          
          }
          
          Class B{
          
          ...
          you can use it with Just A.(ArrayName)
          System.out.println(A.str);
          
          }
          

          希望对你有所帮助。

          【讨论】:

            猜你喜欢
            • 2015-07-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-02-09
            • 1970-01-01
            • 2011-11-07
            • 2013-07-07
            • 1970-01-01
            相关资源
            最近更新 更多