2016-09-03 239 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