-1
我創建了一個新的類,檢查完滿成功代碼: 測試類:
import org.parceler.Parcel;
import io.realm.RealmObject;
import io.realm.TESTRealmProxy;
import io.realm.annotations.PrimaryKey;
@Parcel(implementations = {TESTRealmProxy.class},
value = Parcel.Serialization.BEAN,
analyze = {TEST.class})
public class TEST extends RealmObject {
@PrimaryKey
int Id;
String Name;
public int getId() {
return Id;
}
public String getName() {
return Name;
}
public TEST(){
}
public TEST(int id, String name){
this.Id = id;
this.Name = name;
}
}
活動1:
Intent resultIntent = new Intent(this, NewActivity.class);
TEST dd = new TEST(2, "pa7");
Log.e(TAG, "Profile: "+dd.getId());
resultIntent.putExtra("userProfile",Parcels.wrap(dd));
startActivityForResult(resultIntent, 3);
NewActivity:
TEST mUserd = Parcels.unwrap(getIntent().getParcelableExtra("userProfile"));
if(mProfileUserd != null) {
Log.e(TAG, "ID: USER: " + mUserd.getId()+" - name "+mUserd.getName());
} else {
Log.e(TAG, "Is Null");
}
OUTPUT:
Profile: 2
ID: USER: 0 - name null
OLD
用戶類:
@Parcel(implementations = {
UserRealmProxy.class},
value = Parcel.Serialization.BEAN,
analyze = {User.class})
public class User extends RealmObject {
int Id;
String Name;
}
功能:
Intent resultIntent = new Intent(this, NewActivity.class);
resultIntent.putExtra("userProfile",Parcels.wrap(User.class, mAuthor));
startActivityForResult(resultIntent, 6);
NewActivity:
mUser = Parcels.unwrap((Parcelable) getIntent().getExtras().get("userProfile"));
我有我的對象稱爲用戶,除了一件事情,一切都可以正常工作。我有一個從數據庫請求用戶的片段(B),但我不需要將這些用戶保存在領域中。我在這個片段中唯一需要的是將用戶對象發送給另一個活動。
注意:在其他片段(A)中,我需要保存用戶,但在片段B中我不需要。
我的問題:我是否需要創建另一個名爲「User2」的對象或者不擴展RealmObject並在片段B中使用的對象?或者有另一種方式。
public class User extends RealmObject {
}
我查一下這一點,並再次發佈。感謝您的回答 –
我將包裹實施添加到我的課程中。只有1個對象,所以我不認爲我需要RealmListConverter。無論如何,現在我該如何將對象發送給其他活動?應該是這樣的: resultIntent.putExtra(「user」,(Parcelable)mAuthor); –
我認爲應該可以工作,你應該嘗試一下 – EpicPandaForce