通过2D阵列到另一个活动通过2D阵列到另一个活动(Pass 2D array to another

2019-05-12 07:32发布

如何传递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");

Answer 1:

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

储藏:

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

要访问:

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);


Answer 2:

这最终很适合我:

要开始一个新的活动(发送的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];
    }
}


Answer 3:

可以定义它实现了一个自定义类Parcelable和包含逻辑读取和从/到包裹写2维阵列。 之后,把那个parcelable对象内Bundle运输。



Answer 4:

设置2darray作为公共静态无效。 让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是它最初定义的类。



Answer 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);

}

希望这将有助于你。



文章来源: Pass 2D array to another Activity