用领域填充RecyclerView

时间:2018-03-10 20:00:10

标签: android android-recyclerview realm

我是领域的新手,我想知道这是否是使用领域填充我的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]);
            }


        }
    });
}}

它工作正常,我只是想知道它是否是正确的方法,因为我想使用领域开发我的整个应用程序数据库,所以我不想得到任何将来的问题。

如何使用Realm Studio查看我的数据?

如果出现问题,请告诉我。

1 个答案:

答案 0 :(得分: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();
    }
}