zoukankan      html  css  js  c++  java
  • 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 

  • 相关阅读:
    SharePoint 2013 APP 开发示例 (六)服务端跨域访问 Web Service (REST API)
    麦咖啡导致电脑不能上网
    SharePoint 2013 Central Admin 不能打开
    SharePoint 2013 APP 开发示例 (五)跨域访问 Web Service (REST API)
    SharePoint 2013 APP 开发示例 系列
    synthesize(合成) keyword in IOS
    Git Cmd
    简单的正则匹配
    Dropbox
    SQL Server Replication
  • 原文地址:https://www.cnblogs.com/lee0oo0/p/2699805.html
Copyright © 2011-2022 走看看