在Realm Android中使用List <String>的最佳方式是什么?

我使用RealmString替代Realm Android中的List <String>。 但是我有一个RealmChangeListener的问题。 在下面的例子中,我使用了很多模型中的RealmString,比如Dog,Cat,Chicken。 但是当有人改变数据的时候,所有人都改变了。

例如:当我保存狗 – >狗改变和猫改变。

我可以通过使用RealmDogString,RealmCatString,RealmChickenString来解决这个问题,但它重复我的代码。 有更好的解决scheme吗?

这是我的例子代码:

public class RealmTestActivity extends AppCompatActivity { private Context context; private Realm realm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_realm_test); Stetho.initialize( Stetho.newInitializerBuilder(this) .enableDumpapp(Stetho.defaultDumperPluginsProvider(this)) .enableWebKitInspector(RealmInspectorModulesProvider.builder(this).build()).build()); context = this; RealmConfiguration realmConfig = new RealmConfiguration.Builder(this).build(); Realm.setDefaultConfiguration(realmConfig); realm = Realm.getDefaultInstance(); final RealmResults<Dog> puppies = realm.where(Dog.class).lessThan("age", 2).findAll(); puppies.addChangeListener(new RealmChangeListener<RealmResults<Dog>>() { @Override public void onChange(RealmResults<Dog> results) { System.out.println("XXX Dog ::" + puppies.size()); Toast.makeText(context, "Dog Change", Toast.LENGTH_SHORT).show(); puppies.size(); }}); final RealmResults<Cat> puppiesCat = realm.where(Cat.class).lessThan("age", 2).findAll(); puppiesCat.addChangeListener(new RealmChangeListener<RealmResults<Cat>>() { @Override public void onChange(RealmResults<Cat> results) { System.out.println("XXX Cat ::" + puppiesCat.size()); Toast.makeText(context, "Cat Change", Toast.LENGTH_SHORT).show(); } }); } public void saveDog(View view) { Dog dog = new Dog(); dog.setName("Bella"); dog.setAge(1); RealmList<RealmString> realmStrings = new RealmList<RealmString>(); RealmString realmString = new RealmString(); realmString.setValue("link_Bella.png"); realmStrings.add(realmString); dog.setImage(realmStrings); realm.beginTransaction(); realm.copyToRealm(dog); realm.commitTransaction(); } public void saveCat(View view) { Cat cat = new Cat(); cat.setName("Oliver"); cat.setAge(1); RealmList<RealmString> realmStrings_2 = new RealmList<RealmString>(); RealmString realmString_2 = new RealmString(); realmString_2.setValue("link_Oliver.png"); realmStrings_2.add(realmString_2); cat.setImage(realmStrings_2); realm.beginTransaction(); realm.copyToRealm(cat); realm.commitTransaction(); } } 

Solutions Collecting From Web of "在Realm Android中使用List <String>的最佳方式是什么?"