Android-Realm对象

Android-Realm对象,android,android-fragments,realm,Android,Android Fragments,Realm,更新: 我创建了一个新类,请检查完整代码: 测试等级: 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

更新:

我创建了一个新类,请检查完整代码: 测试等级:

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);
新活动:

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");
        }
输出:

 Profile: 2
 ID:  USER: 0 - name null
旧的

用户类别:

@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);
新活动:

 mUser    = Parcels.unwrap((Parcelable) getIntent().getExtras().get("userProfile"));
我有一个名为User的对象,除了一件事之外,一切正常。我有一个片段(B)从数据库请求用户,但我不需要将那个些用户保存在域中。在这个片段中,我唯一需要的是发送到另一个活动user对象

注意:在其他片段(A)中,我确实需要保存用户,但在片段B中,我不需要

我的问题:我是否需要创建另一个名为“User2”的对象,或者不扩展RealmObject并在片段B中使用的对象?或者还有另一种方法

 public class User extends RealmObject  {
}
两种选择:

1.)保存到数据库并根据ID进行查询

2)使用

输出显示:

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

我会检查这个,然后再发一次。谢谢你的回答,我在课堂上添加了包裹实现。只是一个对象,所以我不认为我需要RealmListConverter。不管怎样,现在我如何才能将对象发送到另一个活动?应该是这样的;:结果:putExtra(“用户”(可包裹)Mautor);我读过:要将@Parcel注释对象用作包裹,必须按Parcels.wrap(object)将它们包裹起来。要从包裹中获取原始对象,请调用Parcels.unwrap(Parcelable)。所以我做了:resultent.putExtra(“用户”,packets.wrap(mautor));然后在我的另一个活动中:mUser=Parcels.unwrap(getIntent().getExtras().getParcelable(“用户”)。。。但不要工作,对象是空的。。。。我以前没有真正使用过Parceler,所以我只能在回家后检查这个问题。如果不将Parceler中的代理指定为类,并将realm.copyFromRealm(Mautor)与Parcels.wrap一起使用,会发生什么情况?我遇到了以下错误:java.lang.IllegalArgumentException:RealmObject无效,因此无法复制。
/* 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;
  }
}
@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