-1

I'm new to realm and i would like to know if this is the right way to use realm to populate my RecyclerView :

public class Offices extends AppCompatActivity {

RecyclerView recyclerView ;
OfficeAdapter officeAdapter;
List<Office> officeDataList;
List<Integer> officesImg;

Cursor cursor;
ArrayList<Office> DatabaseList;
Realm realm;
String[] OfficesNames;
String[] OfficesLocations;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_offices);


    realm = Realm.getDefaultInstance();
    SetPathsData();
    RealmResults <Office> Offices=realm.where(Office.class).findAll();
    officeDataList = new ArrayList<>();
    officeDataList.addAll(realm.copyFromRealm(Offices));

    recyclerView = (RecyclerView) findViewById(R.id.recyclerview_paths);
    officeAdapter = new OfficeAdapter(this , officeDataList);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setAdapter(officeAdapter);

}

private void SetPathsData() {
    OfficesNames = new String[]{"أرونج تورز", "داماس هوليديز", "آرسيما","انجاز","بال تورز","بالكوم","المدينة تورز","بال ترحال","شركة ابناء نمر مرجان للحج والعمرة","شركة مشاعر للحج والعمرة"};
    OfficesLocations = new String[]{"بديا","رام الله","بيت لحم","جنين","سلفيت","طولكرم","نابلس","الخليل","غزة","جنين"};
    officesImg = new ArrayList<>();
    officesImg.add(0,R.drawable.orange);
    officesImg.add(1,R.drawable.damas);
    officesImg.add(2,R.drawable.arsema);
    officesImg.add(3,R.drawable.enjaz);
    officesImg.add(4,R.drawable.paltours);
    officesImg.add(5,R.drawable.palcom);
    officesImg.add(6,R.drawable.almadena);
    officesImg.add(7,R.drawable.palterhal);
    officesImg.add(8,R.drawable.nemer);
    officesImg.add(9,R.drawable.mashaer);

    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            // Add a person

            for (int i = 0; i < 9; i++) {
                Office office = realm.createObject(Office.class ,i);

            office.setOfficeName(OfficesNames[i]);
            office.setOfficeImg(officesImg.get(i));
            office.setOfficeLocation(OfficesLocations[i]);
            }


        }
    });
}}

It's working fine , i just want to know if it's the right way to do it because i want to develop my whole application database using realm , so i don't want get any problem in the future.

How can i see my data using Realm Studio ?

Please let me know if there's something wrong .

hardartcore
  • 16,886
  • 12
  • 75
  • 101
yo aq
  • 5
  • 5
  • To see data store in realm database check [here](https://stackoverflow.com/q/28478987/3145960). And yes, the implementation is okay. – Reaz Murshed Mar 10 '18 at 20:12
  • `officeDataList.addAll(realm.copyFromRealm(Offices));` where do people even get these ideas? – EpicPandaForce Mar 10 '18 at 20:32
  • I'm kinda tempted to just close this as a duplicate of https://stackoverflow.com/questions/40630322/how-to-implement-filterable-in-realmrecyclerviewadapter – EpicPandaForce Mar 10 '18 at 20:41
  • @EpicPandaForce why it's not the same topic and content – yo aq Mar 10 '18 at 21:39
  • You're right, this one is the one I was looking for https://stackoverflow.com/questions/40628152/recyclerview-is-so-slow-with-a-lot-of-item – EpicPandaForce Mar 11 '18 at 09:56
  • Still not the same :/ @EpicPandaForce – yo aq Mar 11 '18 at 15:15
  • @EpicPandaForce could you please tell what's the problem of using officeDataList.addAll(realm.copyFromRealm(Offices)); ? – yo aq Mar 11 '18 at 15:17
  • 1.) `copyFromRealm` already returns an ArrayList 2.) it reads out all items synchronously on UI thread which is slow with larger datasets (like on that question, 5000+ airports freeze the UI for quite some time) and it also stops auto-updating on background thread writes – EpicPandaForce Mar 12 '18 at 08:54

1 Answers1

0
public class RealmInitialData implements Realm.Transaction {
     @Override
     public void execute(Realm realm) {
            String[] OfficesNames = new String[]{"أرونج تورز", "داماس هوليديز", "آرسيما","انجاز","بال تورز","بالكوم","المدينة تورز","بال ترحال","شركة ابناء نمر مرجان للحج والعمرة","شركة مشاعر للحج والعمرة"};
            String[] OfficesLocations = new String[]{"بديا","رام الله","بيت لحم","جنين","سلفيت","طولكرم","نابلس","الخليل","غزة","جنين"};
            List<Integer> officesImg = new ArrayList<>();
            officesImg.add(0,R.drawable.orange);
            officesImg.add(1,R.drawable.damas);
            officesImg.add(2,R.drawable.arsema);
            officesImg.add(3,R.drawable.enjaz);
            officesImg.add(4,R.drawable.paltours);
            officesImg.add(5,R.drawable.palcom);
            officesImg.add(6,R.drawable.almadena);
            officesImg.add(7,R.drawable.palterhal);
            officesImg.add(8,R.drawable.nemer);
            officesImg.add(9,R.drawable.mashaer);

            for (int i = 0; i < 9; i++) {
                Office office = realm.createObject(Office.class ,i);        
                office.setOfficeName(OfficesNames[i]);
                office.setOfficeImg(officesImg.get(i));
                office.setOfficeLocation(OfficesLocations[i]);
            }
    }

    @Override
    public boolean equals(Object obj) {
        return obj != null && obj instanceof RealmInitialData;
    }

    @Override
    public int hashCode() {
        return RealmInitialData.class.hashCode();
    }
}

public class CustomApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Realm.init(this);
        Realm.setDefaultInstance(new RealmConfiguration.Builder()
                                       .initialData(new RealmInitialData())
                                       // ... 
                                      .build());
    }
}

public class Offices extends AppCompatActivity {

    RecyclerView recyclerView;
    OfficeAdapter officeAdapter;
    Realm realm;

    RealmResults<Office> offices;
    RealmChangeListener<RealmResults<Office>> realmChangeListener = new RealmChangeListener<RealmResults<Office>>() { // could also use RealmRecyclerViewAdapter
        @Override
        public void onChange(RealmResults<Office> element) {
            officeAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_offices);

        realm = Realm.getDefaultInstance();
        offices = realm.where(Office.class).findAllAsync();
        offices.addChangeListener(realmChangeListener);

        recyclerView = findViewById(R.id.recyclerview_paths);
        officeAdapter = new OfficeAdapter(this, offices);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(officeAdapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        realm.close();
    }
}

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428