• android intent 传递list或者对象


    Intent newIntent = new Intent();
    newIntent.setClass(this, another.class);
    List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();

    Bundle bundle = new Bundle();
    bundle.putSerializable(”data“, listData );
    newIntent.putExtras(bundle);
    startActivityForResult(newIntent, 0);

    在another类中的oncreate()方法取
    List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    List<Map<String, Object>> listData= (List) bundle.getSerializable(”data“);
    }

    android intent 传递list或者对象

    方法一: 
    如果单纯的传递List<String> 或者List<Integer>的话 就可以直接使用 

    Java代码 

    intent.putStringArrayListExtra(name, value)  

    intent.putIntegerArrayListExtra(name, value)  


    方法二: 
    如果传递的是List<Object>,可以把list强转成Serializable类型,然后通过 
    Java代码  putExtras(key, (Serializable)list)  
    方法传递过去,接受的时候用 
    Java代码  (List<YourObject>) getIntent().getSerializable(key)  
    就可以接受到List<YourObject>数据了 

    但是 切记 你的YourObject类必须要实现Serializable接口 

    方法三: 
    一种是 
    Java代码  Bundle.putSerializable(Key,Object);  
    另一种是 
    Java代码  Bundle.putParcelable(Key, Object);  
    当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口 


    方法四: 
    用intent传来传去 觉得不方便 我们可以写一个在application里面的全局数据 

    1、创建一个属于你自己的android.app.Application的子类 
    2、在manifest中申明一下这个类, 
    3、这时android就为此建立一个全局可用的实例,你可以在其他任何地方使用Context.getApplicationContext()方法获取这个实例,进而获取其中的状态(变量)。 

    继承Application 
    Java代码  

    class MyApp extends Application {  

        private String myState;  

        public String getState(){  

        return myState;  

      }  

      public void setState(String s){  

        myState = s;  

      }  

    }  


    关于AndroidManifest.xml中的配置,原来直接给application加个name就可以了,如下面所示: 
    Java代码  <application android:name=".MyApp"           android:icon="@drawable/icon"  android:label="@string/app_name">  

    使用 
    Java代码  

    class Blah extends Activity {  

        @Override  

      public void onCreate(Bundle b){  

        ...  

        MyApp appState = ((MyApp)getApplicationContext());  

        String state = appState.getState();  

        ...  

      }  

    }  

    ***此为转载,不过感觉自己比较常用利于参开放在这里:http://hi.baidu.com/ihsauqaxblbdmwq/item/dfc9cf9c352b0bdf1a49dfd5 

  • 相关阅读:
    yii2 分页
    yii2 钩子函数
    linux 配置compoer
    Python随心记--迭代器协议和for循环机制
    Python随心记--文件操作处理 open()
    Python随心记--练习
    Python随心记--函数式编程及常用内置函数,及部分实例
    Python随心记--匿名函数
    Python随心记--函数作用域
    Python随心记--局部变量与全局变量
  • 原文地址:https://www.cnblogs.com/leiqun123/p/3164454.html
Copyright © 2020-2023  润新知