android intent 傳遞list或者對象,androidintent

來源:互聯網
上載者:User

android intent 傳遞list或者對象,androidintent

(轉:http://www.cnblogs.com/lee0oo0/archive/2012/09/24/2699805.html)

方法一: 
如果單純的傳遞List<String> 或者List<Integer>的話 就可以直接使用 

Java代碼 

1 intent.putStringArrayListExtra(name, value)  2 intent.putIntegerArrayListExtra(name, value)  

方法二: 
如果傳遞的是List<Object>,可以把list強轉成Serializable類型,然後通過 

1 putExtras(key, (Serializable)list)  

方法傳遞過去,接受的時候用 

1 (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代碼  

1 class MyApp extends Application {  2     private String myState;  3     public String getState(){  4     return myState;  5   }  6   public void setState(String s){  7     myState = s;  8   }  9 } 

關於AndroidManifest.xml中的配置,原來直接給application加個name就可以了,如下面所示: 
Java代碼  <application android:name=".MyApp"           android:icon="@drawable/icon"  android:label="@string/app_name">  

使用 
Java代碼  

1 class Blah extends Activity {  2     @Override  3   public void onCreate(Bundle b){  4     ...  5     MyApp appState = ((MyApp)getApplicationContext());  6     String state = appState.getState();  7     ...  8   }  9 }  

***此為轉載,不過感覺自己比較常用利於參開放在這裡:http://hi.baidu.com/ihsauqaxblbdmwq/item/dfc9cf9c352b0bdf1a49dfd5

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.