Android 使用领域填充RecyclerView
我是realm的新手,我想知道这是否是使用realm填充我的RecyclerView的正确方法:Android 使用领域填充RecyclerView,android,android-recyclerview,realm,Android,Android Recyclerview,Realm,我是realm的新手,我想知道这是否是使用realm填充我的RecyclerView的正确方法: public class Offices extends AppCompatActivity { RecyclerView recyclerView ; OfficeAdapter officeAdapter; List<Office> officeDataList; List<Integer> officesImg; Cursor cursor; ArrayList&l
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]);
}
}
});
}}
公共类办公室扩展应用程序活动{
回收视图回收视图;
OfficeAdapter OfficeAdapter;
助产士名单;
办事处名单;
光标;
ArrayList数据库列表;
境界;
字符串[]办公室名称;
字符串[]办公地点;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、活动和办公室);
realm=realm.getDefaultInstance();
SetPathsData();
RealmResults Offices=realm.where(Office.class).findAll();
OfficeCatalist=新的ArrayList();
addAll(realm.copyFromRealm(Offices));
recyclerView=(recyclerView)findViewById(R.id.recyclerView\u路径);
officeAdapter=新的officeAdapter(这是OfficeCatalist);
RecyclerView.LayoutManager mLayoutManager=新的LinearLayoutManager(此);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(officeAdapter);
}
私有void SetPathsData(){
各政府官员名称=新字符串(新字符串)的新字符串(新字符串)的新字符串(新字符串)的各各各各各各履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履履方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方在各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各ع;
新的字符串;新的字符串;新的字符串;新的政府办公地点;新的字符串;新的字符串;新的字符串;新的字符串;新的字符串;新的字符串;新的字符串;新的字符串;新的字符串;新的字符串;新的字符串;新的[[[[[[[[[[[[7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7])))、)、新的;新的;新的;新的;新字符串,新的[[[[[[[[[[[[[[[[[[[[[[[[[[6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6]]"",;
officesImg=新的ArrayList();
办公室模拟添加(0,右侧可绘制橙色);
办公室模拟添加(1,R.可提取damas);
办公室模拟添加(2,R.drawable.arsema);
办公室模拟添加(3,R.drawable.enjaz);
办公室模拟添加(4,R.可提取的paltours);
officesImg.add(5,R.drawable.palcom);
办公室模拟添加(6,R.drawable.almadena);
办公室模拟添加(7,R.drawable.palterhal);
办公室模拟添加(8,R.可提取nemer);
办公室模拟添加(9,R.drawable.mashaer);
realm.executeTransaction(新realm.Transaction(){
@凌驾
公共void执行(领域){
//添加一个人
对于(int i=0;i<9;i++){
Office=realm.createObject(Office.class,i);
office.setOfficeName(OfficesNames[i]);
office.setOfficeImg(officesImg.get(i));
办公地点设置办公地点(办公地点[i]);
}
}
});
}}
它工作得很好,我只是想知道它是否是正确的方法,因为我想使用realm开发我的整个应用程序数据库,所以我不想在将来遇到任何问题
如何使用Realm Studio查看数据
如果有什么问题,请告诉我
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();
}
}
公共类办公室扩展应用程序活动{
回收视图回收视图;
OfficeAdapter OfficeAdapter;
境界;
RealmResults办公室;
RealmChangeListener RealmChangeListener=新的RealmChangeListener(){//也可以使用RealmRecyclServiceAdapter
@凌驾
更改后的公共void(RealmResults元素){
officeAdapter.notifyDataSetChanged();
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、活动和办公室);
realm=realm.getDefaultInstance();
offices=realm.where(Office.class).findalsync();
offices.addChangeListener(realmChangeListener);
recyclerView=findViewById(R.id.recyclerView\u路径);
officeAdapter=新的officeAdapter(本,办公室);
recyclerView.setLayoutManager(新的LinearLayoutManager(本));
recyclerView.setAdapter(officeAdapter);
}
@凌驾
受保护的空onDestroy(){
super.ondestory();
realm.close();
}
}
查看域数据库检查中的数据存储。是的,实现还可以代码>人们是从哪里得到这些想法的?我有点想把它作为@EpicPandaForce的复制品来结束,为什么它的主题和内容不一样你是对的,这就是我一直在寻找的
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();
}
}