2016-09-03 139 views
-1

更新:Android的 - 領域對象

我創建了一個新的類,檢查完滿成功代碼: 測試類:

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 { 
} 

回答

0

兩個選項:

1)保存到數據庫和查詢基於ID

2)使用Parceler library and implement Parcelable

@Parcel(implementations = { UserRealmProxy.class }, 
     value = Parcel.Serialization.BEAN, 
     analyze = { User.class }) 
public class User extends RealmObject { 
    // ... 
} 

compile "org.parceler:parceler-api:1.1.5" 
apt "org.parceler:parceler:1.1.5" 

parcel RealmList, use following code

/* Copyright 2016 Patrick Löwenstein 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
* http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. */ 

public class RealmListParcelConverter implements TypeRangeParcelConverter<RealmList<? extends RealmObject>, RealmList<? extends RealmObject>> { 
    private static final int NULL = -1; 

    @Override 
    public void toParcel(RealmList<? extends RealmObject> input, Parcel parcel) { 
    if (input == null) { 
     parcel.writeInt(NULL); 
    } else { 
     parcel.writeInt(input.size()); 
     for (RealmObject item : input) { 
     parcel.writeParcelable(Parcels.wrap(item), 0); 
     } 
    } 
    } 

    @Override 
    public RealmList fromParcel(Parcel parcel) { 
    int size = parcel.readInt(); 
    RealmList list = new RealmList(); 

    for (int i=0; i<size; i++) { 
     Parcelable parcelable = parcel.readParcelable(getClass().getClassLoader()); 
     list.add((RealmObject) Parcels.unwrap(parcelable)); 
    } 

    return list; 
    } 
} 

通常我會告訴你只要copyFromRealm()併發送它,但是你必須以某種方式序列化它,而你需要Parcelable

編輯:好吧,我已經嘗試並測試了以下內容:

@Parcel(implementations = { PostRealmProxy.class }, 
     value = Parcel.Serialization.BEAN, 
     analyze = { Post.class }) 
public class Post extends RealmObject { 
    @PrimaryKey 
    private long id; 

    private String text; 

    public long getId() { 
     return id; 
    } 

    public void setId(long id) { 
     this.id = id; 
    } 

    public String getText() { 
     return text; 
    } 

    public void setText(String text) { 
     this.text = text; 
    } 
} 

public void startNextActivity(View view) { 
    Log.i("MAINACTIVITY", "START NEXT ACTIVITY"); 
    Post post = new Post(); 
    post.setId(41274); 
    post.setText("Blaaaaaah"); 
    Intent intent = new Intent(this, SecondActivity.class); 
    intent.putExtra("post", Parcels.wrap(post)); 
    startActivity(intent); 
} 

public class SecondActivity 
     extends RealmActivity { 
    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //... 
     Post post = Parcels.unwrap(getIntent().getParcelableExtra("post")); 
     Log.i("SECOND", "" + post.getId()); 
     Log.i("SECOND", "" + post.getText()); 
    } 
} 

輸出說道:

09-05 23:54:09.343 12085-12085/com.zhuinden.realmdatabind I/MAINACTIVITY: START NEXT ACTIVITY 
09-05 23:54:09.383 12085-12085/com.zhuinden.realmdatabind I/SECOND: 41274 
09-05 23:54:09.383 12085-12085/com.zhuinden.realmdatabind I/SECOND: Blaaaaaah 
+0

我查一下這一點,並再次發佈。感謝您的回答 –

+0

我將包裹實施添加到我的課程中。只有1個對象,所以我不認爲我需要RealmListConverter。無論如何,現在我該如何將對象發送給其他活動?應該是這樣的: resultIntent.putExtra(「user」,(Parcelable)mAuthor); –

+0

我認爲應該可以工作,你應該嘗試一下 – EpicPandaForce