Search code examples
androidrealm

Should I use copyFromRealm() for populating a Spinner?


I have read a few answers and tutorials about when to use copyFromRealm but I am still unsure if my understanding is correct; which is, use copyFromRealm when you want to have untethered Objects (if you change them, nothing will change in Realm DB).

In my case I have a Spinner and I want to populate it with values from Realm:

Spinner mySpinner = ...;

List<MyObjects> myObjects = RealmInstance.getInstance().getRealm().where(MyObject.class).findAll();
myObjects = RealmInstance.getInstance().getRealm().copyFromRealm(myObjects); // do I need this here?

List<String> entries = new ArrayList<>();
for(MyObject t : myObjects)
    entries.add(t.getName() + " (" + t.getDesc() + ")");

ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, entries);

mySpinner.setAdapter(adapter);

I think I don't need to copy the objects to an unmanaged list since I will not modify them and I am anyways copying the values to a separate array (entries).

Is my thinking right here?


Solution

  • For Spinners, if you don't intend to change things directly, then you can use RealmBaseAdapter, as long as you customize getDropDownView.

    @Override
    public View getDropDownView(int position, @Nullable View convertView,
            @NonNull ViewGroup parent) {
        ...
    }