Android編程實(shí)現(xiàn)使用Intent傳輸包含自定義類的ArrayList示例
本文實(shí)例講述了Android編程實(shí)現(xiàn)使用Intent傳輸包含自定義類的ArrayList。分享給大家供大家參考,具體如下:
前言
之前項(xiàng)目中通過Intent只是傳輸簡(jiǎn)單的字符串,這次因?yàn)樾枰谇耙粋€(gè)頁面聯(lián)網(wǎng)獲取對(duì)象數(shù)據(jù),然后在下一個(gè)頁面使用,所以考慮到使用Intent傳輸包含自定義類的ArrayList。
Serializable
Java的對(duì)象序列化指的是將那些實(shí)現(xiàn)了Serializable接口的對(duì)象轉(zhuǎn)換成一個(gè)字節(jié)序列,并且能在需要的時(shí)候再將這個(gè)字節(jié)序列完全恢復(fù)為之前的對(duì)象。
想實(shí)現(xiàn)對(duì)象的序列化,需要實(shí)現(xiàn)java.io.Serializable接口(注意,這個(gè)接口只是一個(gè)標(biāo)記接口,并沒有具體需要override的方法)。當(dāng)然,你也可以自己實(shí)現(xiàn)對(duì)象的序列化,但是我認(rèn)為既然Java提供了這么一套對(duì)象序列化的機(jī)制,我們最好還是使用官方提供的方法。
Example
創(chuàng)建一個(gè)簡(jiǎn)單對(duì)象,并且實(shí)現(xiàn)Serializable接口
package javastudy;
import java.io.Serializable;
public class Person implements Serializable {
private static final long serialVersionUID = -6470574927973900913L;
private String firstName;
private String secondName;
// example for transinet
private transient String noSerializableString;
public Person(String firstName, String secondName, String noSerializableString) {
super();
this.firstName = firstName;
this.secondName = secondName;
this.noSerializableString = noSerializableString;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSecondName() {
return secondName;
}
public void setSecondName(String secondName) {
this.secondName = secondName;
}
public String getNoSerializableString() {
if (noSerializableString != null) {
return noSerializableString;
} else {
return "";
}
}
public void setNoSerializableString(String noSerializableString) {
this.noSerializableString = noSerializableString;
}
public String toString() {
return "Person [ first name :" + getFirstName() + ", second name :" + getSecondName() + ", no serializable :"
+ getNoSerializableString() + "]";
}
}
再寫一個(gè)類,用于實(shí)現(xiàn)對(duì)象的序列化和反序列化
package javastudy;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class TestSerializable {
public static void main(String[] args) {
Person person = new Person("Wang", "Zhengyi", "Genius");
String fileName = "/tmp/person.out";
// save object to file
FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
fos = new FileOutputStream(fileName);
out = new ObjectOutputStream(fos);
out.writeObject(person);
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// read object from file
FileInputStream fin = null;
ObjectInputStream in = null;
try {
fin = new FileInputStream(fileName);
in = new ObjectInputStream(fin);
Person p = (Person) in.readObject();
System.out.println(p);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Intent傳輸包含自定義類的ArrayList
之所以之前介紹了Serializable,是因?yàn)檫@是實(shí)現(xiàn)Intent傳輸?shù)那疤?,ArrayList包含的自定義類必須實(shí)現(xiàn)Serializable接口才能通過putSerializable()方法被傳遞。
還是用上面的Person類作為自定義的類,則第一個(gè)傳遞ArrayList的Activity關(guān)鍵代碼如下:
// Intent Creation and Initialization
Intent passIntent = new Intent();
passIntent.setClass(MainActivity.this, SecondaryActivity.class);
// Create custom class Object
Person p1 = new Person("Wang", "Zhengyi", "first");
Person p2 = new Person("Chen", "Shan", "second");
// Create Array List of custom Class and add the Created object
ArrayList<Person> aListClass = new ArrayList<Person>();
aListClass.add(p1);
aListClass.add(p2);
// Create a Bundle and Put Bundle in to it
Bundle bundleObject = new Bundle();
bundleObject.putSerializable("key", aListClass);
// Put Bundle in to Intent and call start Activity
passIntent.putExtras(bundleObject);
startActivity(passIntent);
第二個(gè)接收ArrayList的Activity關(guān)鍵代碼如下:
try{
// Get the Bundle Object
Bundle bundleObject = getIntent().getExtras();
// Get ArrayList Bundle
ArrayList<Person> classObject = (ArrayList<Person>) bundleObject.getSerializable("key");
Retrieve Objects from Bundle
for(int index = 0; index < classObject.size(); index++){
Person person = classObject.get(index);
Toast.makeText(getApplicationContext(), person, Toast.LENGTH_SHORT).show();
}
} catch(Exception e){
e.printStackTrace();
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android實(shí)現(xiàn)Listview異步加載網(wǎng)絡(luò)圖片并動(dòng)態(tài)更新的方法
這篇文章主要介紹了Android實(shí)現(xiàn)Listview異步加載網(wǎng)絡(luò)圖片并動(dòng)態(tài)更新的方法,結(jié)合實(shí)例形式詳細(xì)分析了ListView異步加載數(shù)據(jù)的操作步驟與具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-08-08
Android AsyncTask實(shí)現(xiàn)機(jī)制詳細(xì)介紹及實(shí)例代碼
這篇文章主要介紹了Android AsyncTask實(shí)現(xiàn)機(jī)制詳細(xì)介紹及實(shí)例代碼的相關(guān)資料,這里附有示例代碼,幫助大家學(xué)習(xí)理解,需要的朋友可以參考下2016-12-12
解析Android開發(fā)優(yōu)化之:對(duì)界面UI的優(yōu)化詳解(二)
在一個(gè)應(yīng)用程序中,一般都會(huì)存在多個(gè)Activity,每個(gè)Activity對(duì)應(yīng)著一個(gè)UI布局文件。一般來說,為了保持不同窗口之間的風(fēng)格統(tǒng)一,在這些UI布局文件中,幾乎肯定會(huì)用到很多相同的布局2013-05-05
淺談Android Studio導(dǎo)出javadoc文檔操作及問題的解決
這篇文章主要介紹了淺談Android Studio導(dǎo)出javadoc文檔操作及問題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
android獲取手機(jī)IMSI碼判斷手機(jī)運(yùn)營(yíng)商代碼實(shí)例
這篇文章主要介紹了android獲取手機(jī)IMSI碼判斷手機(jī)運(yùn)營(yíng)商代碼實(shí)例,大家參考使用2013-11-11
Android系統(tǒng)中的藍(lán)牙連接程序編寫實(shí)例教程
這篇文章主要介紹了Android系統(tǒng)中的藍(lán)牙連接程序編寫實(shí)例教程,包括藍(lán)牙的設(shè)備查找及自動(dòng)配對(duì)等各種基礎(chǔ)功能的實(shí)現(xiàn),十分給力,需要的朋友可以參考下2016-04-04
Android手機(jī)衛(wèi)士之獲取聯(lián)系人信息顯示與回顯
這篇文章主要介紹了Android手機(jī)衛(wèi)士之獲取聯(lián)系人信息顯示與回顯的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Android上實(shí)現(xiàn)easyconfig(airkiss)方法
本篇文章主要給大家講解了在Android系統(tǒng)上實(shí)現(xiàn)easyconfig(airkiss)的方法,有這方面需要的朋友參考學(xué)習(xí)下吧。2018-01-01

