2016-09-03 9 views
-1

UPDATEDオブジェクト:アンドロイド - レルムが

私は新しいクラスを作成し、のcompletコードチェック: TESTクラス:

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

Activity1:

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

Userクラス:

@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")); 

私はUserというオブジェクトを持っていますが、1つのこと以外はすべて正常に動作します。私はデータベースからユーザーを要求する断片(B)を持っていますが、そのユーザーを領域に保存する必要はありません。私がこの断片で必要とするのは、ユーザオブジェクトを別のアクティビティに送ることだけです。

注:他のフラグメント(A)では、ユーザーを保存する必要がありますが、フラグメントBでは必要ありません。

私の質問:RealmObjectを拡張せず、フラグメントBで使用する「User2」という別のオブジェクトを作成する必要がありますか?または別の方法があります。

public class User extends RealmObject { 
} 

答えて

0

つのオプション:

1))

2. IDに基づいて、DBとクエリに保存使用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が必要です。

EDIT:オーケー私が試したし、次のようにテストしてみた:

@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

私のクラスにパーセルの実装を追加しました。私はRealmListConverterが必要とは思わないので、ただ1つのオブジェクトです。とにかく、どうやってオブジェクトを別のアクティビティに送ることができますか?次のようにする必要があります: resultIntent.putExtra( "user"、(Parcelable)mAuthor); –

+0

私はそれがうまくいくはずです、あなたはそれを試してみるべきだと思います – EpicPandaForce