【发布时间】:2018-03-10 20:00:10
【问题描述】:
我是领域的新手,我想知道这是否是使用领域来填充我的 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 查看我的数据?
如果有什么问题请告诉我。
【问题讨论】:
-
查看领域数据库中的数据存储检查here。是的,实现没问题。
-
officeDataList.addAll(realm.copyFromRealm(Offices));人们从哪里得到这些想法? -
我有点想把它作为stackoverflow.com/questions/40630322/…的副本关闭
-
@EpicPandaForce 为什么主题和内容不一样
-
你说得对,这个就是我要找的那个stackoverflow.com/questions/40628152/…
标签: android android-recyclerview realm