我的计划是列出一些学校,当我点击一所学校时,它会列出学生的姓名和身份证.我正在使用Realm.
我能够列出学校并将学生的结果隐藏在realmResult中,但我无法将其传递给另一项活动.
我想将realmResult转换为数组列表并将其传递给另一个带有数组适配器的活动.任何人都可以给我一些线索吗?….谢谢.
public class School extends RealmObject {
@Required
private String SchoolID;
private String SchoolName;
private RealmList<Student> Students;
getters / setters
}
public class Student extends RealmObject{
@Required
private String StudentID;
private String StudentName;
}
主要活动
…..
RealmResults<School> schools = realm.where(School.class).findAll();
final SchoolAdapter adapter = new SchoolAdapter(this, R.id.schools_list, schools, true);
ListView listView = (ListView) findViewById(R.id.schools_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
RealmList list = adapter.getRealmResults().get(position).getStudents();
RealmResults test = list.where().findAll();
....
学校适配器
public class SchoolAdapter extends RealmBaseAdapter<School> implements ListAdapter {
private static class ViewHolder{
TextView school;
}
public SchoolAdapter(Context context, int resId, RealmResults<School> realmResults, boolean automaticUpdate) {
super(context, realmResults, automaticUpdate);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
viewHolder = new ViewHolder();
viewHolder.school = (TextView) convertView.findViewById(android.R.id.text1);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
School item = realmResults.get(position);
viewHolder.school.setText(item.getSchoolName());
return convertView;
}
public RealmResults<School> getRealmResults() {
return realmResults;
}
}
解决方法:
谢谢你的回复.我想我没有正确地初始化这个领域.
关于主要活动:
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this).build();
Realm.deleteRealm(realmConfiguration);
Realm.setDefaultConfiguration(realmConfiguration);
realm = Realm.getDefaultInstance();
关于学生活动,我应该在“On Create”中有以下内容
realm = Realm.getDefaultInstance();