简体   繁体   English

无法转换RealmList <T> 到JSONObject字符串android

[英]Can not convert RealmList<T> to JSONObject string android

I have 2 Activity, I want to send data from activity 1 to Activity 2 when clicking a list item. 我有2个活动,我想在单击列表项时将数据从活动1发送到活动2。 On clicking i can get the data mapped to that ListItem, that data is a kind of RealmList type. 单击我可以获取映射到该ListItem的数据,该数据是一种RealmList类型。 I used Gson library to convert my model to Json String. 我使用Gson库将模型转换为Json String。 I can achieve this if my data is a type of List but am using RealmList. 如果我的数据是List类型,但我正在使用RealmList,则可以实现此目的。 When i try to Achieve the following it my application gets freezed. 当我尝试实现以下目标时,我的应用程序被冻结。

This is my filter Model : 这是我的过滤器模型:

    Filter filter = new Filter();
    filter.setTitle("");
    filter.setId("");
    filter.setCode("");

RealmList is like : RealmList就像:

    RealmList<Filter> list = new RealmList<Filter>();

I tried to convert above list to JSONObject string as follows : 我试图将上述列表转换为JSONObject字符串,如下所示:

 new Gson().toJson(filter))

Above live giving me the following error : 以上直播给我以下错误:

 PID: 12959 Reason: Input dispatching timed out (Waiting to send non-key event because the touched window has not finished processing certain input events that were delivered to it over 500.0ms ago.  Wait queue length: 4.  Wait queue head age: 5647.8ms.)
                                            Load: 10.67 / 9.63 / 9.38
                                            CPU usage from 1103ms to -6084ms ago:
                                              101% 12959/com.anubavam.creatrix: 98% user + 3.4% kernel / faults: 6335 minor 15 major
                                              44% 832/system_server: 25% user + 18% kernel / faults: 8128 minor 104 major
                                              10% 1623/com.android.phone: 5.5% user + 5.4% kernel / faults: 4275 minor 57 major
                                              3.6% 4304/android.process.media: 1.7% user + 1.9% kernel / faults: 4931 minor 73 major
                                              0% 286/debuggerd: 0% user + 0% kernel / faults: 3205 minor 38 major
                                              5.8% 91/kswapd0: 0% user + 5.8% kernel
                                              5.8% 1272/com.android.systemui: 3.8% user + 1.9% kernel / faults: 2852 minor 39 major
                                              4.5% 302/zygote: 3% user + 1.5% kernel / faults: 11239 minor
                                              3.4% 1580/com.motorola.process.system: 2.3% user + 1.1% kernel / faults: 1881 minor 1 major
                                              3.3% 153/mmcqd/0: 0% user + 3.3% kernel

Note List and ArrayList does not give me any error, its working file.But RealmList is the problem. 注意List和ArrayList不会给我任何错误,它的工作文件。但是RealmList是问题所在。 I tries following line also, but no use. 我也尝试以下行,但没有用。

    List<Filter> list = new RealmList<Filter>();

can any one have work around for this. 任何人都可以为此解决。

As of now, Realm objects are not Serializable , See this answer . 到目前为止,Realm对象不可Serializable ,请参Serializable 答案 That means you cannot pass this object through Intent to other activity or converting it to JSON using GSON library. 这意味着您无法通过Intent将此对象传递给其他活动,也不能使用GSON库将其转换为JSON

However, there are two ways using which you can have your model in your second activity. 但是,可以通过两种方式在第二个活动中使用模型。

  1. Pass the primary key of this model through Intent and in second 通过Intent传递此模型的主键
    activity fetch the model again from DB using this key. 活动使用此密钥再次从数据库获取模型。
  2. You can this Parceler library for serializing the object and passing to second activity. 您可以使用此Parceler库将对象序列化并传递给第二个活动。 Check this answer for using Parceler . 检查此答案以使用Parceler

Realm currently does not support passing data through Intent. Realm当前不支持通过Intent传递数据。 Options available are 可用的选项有

1) Send some unique Id to second Activity and requery for the same object on the second Activity 1)向第二个Activity发送一些唯一的ID,然后在第二个Activity中重新查询同一对象

2) Also by using Third party Parceler 2)也使用第三方包裹

You may need to add the dependencies as well 您可能还需要添加依赖项

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

I want to send data from activity 1 to Activity 2 when clicking a list item. 单击列表项时,我想将数据从活动1发送到活动2。

Parcelling data and creating unmanaged/detached copies of data is a misuse of Realm. 拆分数据并创建非托管/分离的数据副本是对Realm的滥用。

Send the primary key of the data through the intent, and requery the object in the second activity. 通过该意图发送数据的主键,然后在第二个活动中重新查询该对象。

Intent intent = new Intent(this, OtherActvity.class);
intent.putExtra("itemId", listItem.getId());
startActivity(intent);

public class OtherActivity extends AppCompatActivity {
    Realm realm;

    ListItem listItem;

    @Override
    public void onCreate(Bundle bundle) {
        realm = Realm.getDefaultInstance();
        super.onCreate(bundle);
        listItem = realm.where(ListItem.class).equalTo("id", getIntent().getExtras().getString("itemId")).findFirst(); // re-query
        // ...
    }

    @Override
    public void onDestroy() {
        realm.close();
        realm = null;
        super.onDestroy();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM