[英]Send List Objects using Intent from one Activity to another using Parcelable | Android
[英]Using parcelable and intent in the same activity
我在同一活動中使用2個意圖。其中一個用於輸入名字,姓氏和ID。其他意圖用於將數據發送到另一個活動。 但是,當我使用類似的意圖時,這些代碼將無法正常工作。 我可以使用這樣的意圖嗎
intent.putExtra("lastName", intent2.getString("lastName"));
搜索活動
Intent intent = new Intent(getApplicationContext(),SearchActivity.class);
Bundle b=new Bundle();
b.putString("firstName", firstName);
b.putString("lastName", lastName);
b.putString("id", id);
b.putParcelable("LoginPartClass",lp2);
intent.putExtras(b);
startActivity(intent);
搜索活動代碼
final Bundle intent2 = getIntent().getExtras();
Intent intent = new Intent(getApplicationContext(),AnotherActivity.class);
intent.putExtra("keywords", keywords);
if(intent2!=null) {
final String Firstname=intent2.getString("firstName");
intent.putExtra("firstName", Firstname); }
intent.putExtra("lastName", intent2.getString("lastName"));
intent.putExtra("id", intent2.getString("id"));
}
startActivity(intent);
可分割類
public class LoginPart implements Parcelable{
private Token requestoken;
private OAuthService s;
private String authURL;
public LoginPart(Token Token, OAuthService S, String AuthURL) {
requestoken = Token;
s = S;
authURL = AuthURL;
}
public LoginPart( ) {
}
public Token getRequestoken() {
return requestoken;
}
public void setRequestoken(Token requestoken) {
this.requestoken = requestoken;
}
public OAuthService getS() {
return s;
}
public Parcelable getSParcelable() {
return (Parcelable) s;
}
public void setS(OAuthService s) {
this.s = s;
}
public String getAuthURL() {
return authURL;
}
public void setAuthURL(String authURL) {
this.authURL = authURL;
}
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeValue(requestoken);
dest.writeValue(s);
dest.writeValue(authURL);
}
}
問題與Parcable類有關,當我使用parceable類時發生崩潰,我該如何解決?
在您的第一個活動中:
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
Bundle b = new Bundle();
b.putInt("id", 1);
b.putString("firstName", "this is my first name");
b.putString("lastName", "this is my last name");
intent.putExtras(b);
startActivity(intent);
在第二個活動中:
Bundle b =getIntent().getExtras();
String name =b.getString("firstName");
String lastname =b.getString("lastName");
像這樣攪動您的LoginPart類...
public class LoginPart implements Parcelable {
private Token requestoken;
private OAuthService s;
private String authURL;
public LoginPart(Token Token, OAuthService S, String AuthURL) {
requestoken = Token;
s = S;
authURL = AuthURL;
}
public LoginPart( ) {
}
public LoginPart(Parcel parcel) {
requestoken = parcel.readValue(getClassLoader());
s = parcel.readValue(getClassLoader());
authURL = parcel.readValue(getClassLoader());
}
public Token getRequestoken() {
return requestoken;
}
public void setRequestoken(Token requestoken) {
this.requestoken = requestoken;
}
public OAuthService getS() {
return s;
}
public Parcelable getSParcelable() {
return (Parcelable) s;
}
public void setS(OAuthService s) {
this.s = s;
}
public String getAuthURL() {
return authURL;
}
public void setAuthURL(String authURL) {
this.authURL = authURL;
}
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeValue(requestoken);
dest.writeValue(s);
dest.writeValue(authURL);
}
public static final Parcelable.Creator<LoginPart> CREATOR = new Creator<SecondActivity.LoginPart>() {
@Override
public LoginPart[] newArray(int size) {
return new LoginPart[size];
}
@Override
public LoginPart createFromParcel(Parcel source) {
return new LoginPart(source);
}
};
}
為此,您的Token類和OAuthService類應實現Parcelable
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.