編輯:關於Android編程
假設對象為People類,包含信息姓名和年齡:
public class People{
public String strName;
public int iAge;
public People(String strName,int iAge){
this.strName = strName;
this.iAge = iAge;
}
public String getName(){
return strName;
}
public int getAge(){
return iAge;
}
}
方法一:Serializable
必須條件:類實現了Serializable接口
public class People implements Serializable{
private static final long serialVersionUID = 1L;
public String strName;
public int iAge;
public People(String strName,int iAge){
this.strName = strName;
this.iAge = iAge;
}
public String getName(){
return strName;
}
public int getAge(){
return iAge;
}
}
傳遞對象:
傳遞端:
People people = new People("John", 21);
Intent intent = new Intent(SendActivity.this,RcvActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("people", people);
intent.putExtras(bundle);
startActivity(intent);
接收端:
People people = (People) this.getIntent().getSerializableExtra("people");
String strData = people.getName() + " " + people.getAge();
Toast.makeText(getApplication(),strData, Toast.LENGTH_SHORT).show();
傳遞對象數組:
傳遞端:
Listpeople = new ArrayList (); people.add(new People("John", 21)); people.add(new People("Amy", 20)); Bundle bundle = new Bundle(); bundle.putSerializable("people", (Serializable) people); Intent intent = new Intent(SendActivity.this, RcvActivity.class); intent.putExtras(bundle); startActivity(intent);
接收端:
ListresultList = (List ) this.getIntent().getSerializableExtra("people"); String strData = ""; for (People p : resultList) { strData = strData + p.strName + " " + p.iAge + "\n"; } Toast.makeText(getApplication(), strData, Toast.LENGTH_SHORT).show();
方法二:Parcelable
必須條件:類實現了Parcelable接口
public class People implements Parcelable {
public String strName;
public int iAge;
public People(String strName,int iAge){
this.strName = strName;
this.iAge = iAge;
}
public String getName(){
return strName;
}
public int getAge(){
return iAge;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int arg1) {
// TODO Auto-generated method stub
parcel.writeString(strName);
parcel.writeInt(iAge);
}
public static final Parcelable.Creator CREATOR = new Creator() {
public People createFromParcel(Parcel source) {
People pTemp = new People("",0);
pTemp.strName = source.readString();
pTemp.iAge = source.readInt();
return pTemp;
}
public People[] newArray(int size) {
return new People[size];
}
};
}
傳遞對象:
傳遞端:People people = new People("John", 21);
Intent intent = new Intent(SendActivity.this,RcvActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("people", people);
intent.putExtras(bundle);
startActivity(intent);
接收端:
People people = (People) this.getIntent().getParcelableExtra("people");
String strData = people.getName() + " " + people.getAge();
Toast.makeText(getApplication(),strData, Toast.LENGTH_SHORT).show();
傳遞對象數組:
傳遞端:
ListPeople = new ArrayList (); People.add(new People("John", 21)); People.add(new People("Amy", 20)); Intent intent = new Intent(SendActivity.this,RcvActivity.class); Bundle bundle = new Bundle(); bundle.putParcelableArrayList("People", (ArrayList extends Parcelable>) People); intent.putExtras(bundle); startActivity(intent);
接收端:
ListresultList = this.getIntent().getExtras() .getParcelableArrayList("People"); String strData = ""; for (People p : resultList) { strData = strData + p.strName + " " + p.iAge + "\n"; } Toast.makeText(getApplication(), strData, Toast.LENGTH_SHORT).show();
可以發現在Parcelable中需實現public int describeContents()、 publicvoid writeToParcel(Parcel parcel, int arg1),還需要在添加一個靜態成員變量CREATOR:public static final Parcelable.Creator
區別(by: http://www.cnblogs.com/renqingping/archive/2012/10/25/Parcelable.html)
1.Serializable的實現,只需要implements Serializable即可。這只是給對象打了一個標記,系統會自動將其序列化。
2.Parcelabel的實現,不僅需要implements Parcelabel,還需要在類中添加一個靜態成員變量CREATOR,這個變量需要實現 Parcelable.Creator 接口。
3.在使用內存的時候,Parcelable 類比Serializable性能高,所以推薦使用Parcelable類。4.Serializable在序列化的時候會產生大量的臨時變量,從而引起頻繁的GC。
5.Parcelable不能使用在要將數據存儲在磁盤上的情況,因為在外界有變化的情況下Parcelable不能很好的保證數據的持續性。
Android開發-Retrofit-AndroidStudio(一)百度首頁解析
有能力的同學可以直接去看 Retrofit官方Demo:https://github.com/square/retrofit我這邊簡單使用一下,以百度首頁解析作為開篇:導
搶紅包插件實現原理淺析
搶紅包,先看效果圖~ 實現自動搶紅包,解決問題有兩點: 一:如何實時監聽發紅包的事件 二:如何在紅包到來的時候自動進入頁面並自動點擊紅包一、如何獲取紅包到來的事件
Android FotoMix
the xml layout file is simple.u just need a imageview: activity_main.xml:
面試准備android(一)
在牛客(一個很多筆試面試交流的平台,感覺每天一套可以萌萌哒(☆_☆))上看到一個大神,簡直是offer收割機TAT,其面經中好多東西都是基礎,覺得自己有必要總結並學習我不