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 

    class MyApp extends Application {  
        private String myState;  
        public String getState(){  
        return myState;  
      }  
      public void setState(String s){  
        myState = s;  
      }  
    }  

    关于AndroidManifest.xml中的配置,原来直接给application加个name就可以了,如下面所示: 

    <application android:name=".MyApp"           android:icon="@drawable/icon"  android:label="@string/app_name">

    使用 

    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

  • 相关阅读:
    Natas Wargame Level 13 Writeup(文件上传漏洞,篡改file signature,Exif)
    Natas Wargame Level 12 Writeup(文件上传漏洞)
    Natas Wargame Level 9 Writeup(bash injection)
    Natas Wargame Level 2 Writeup 与目录泄露(强制访问)
    Natas Wargame Level 3 Writeup 与 robots.txt
    字符编码与文件操作
    python 基本数据类型
    python数据类型内置方法 字符串和列表
    python常用模块
    python数据类型及基本运算符
  • 原文地址:https://www.cnblogs.com/sishuiliuyun/p/3879025.html
Copyright © 2011-2022 走看看