我正在尝试通过使用查询来使用 Realm 检索最喜欢的学校及其最喜欢的学生来制作带有标题的 ListView 。
中学点灰色
肯特
简
我的问题是我是否可以在 Realm 对象中实现方法,以便我可以使用该方法来查明 Realm 结果是标题还是项目,如果无法完成,我如何绕过并仍然打印标题以及来自 Realm 结果的 ListView 。
我在编译下面的代码时也有错误。它说“getters isHeader 不与任何字段相关联”
我的界面类:
public interface Item {
boolean isHeader();
}
我的模型类:
public class School extends RealmObject implements Item{
@Required
private String SchoolID;
private String SchoolName;
private RealmList<Student> Students;
@Ignore
private boolean answerSchool = true;
public boolean isHeader() {
return answerSchool;
}
getters/ setters
}
public class Student extends RealmObject implements Item {
@Required
private String StudentID;
private String StudentName;
private Boolean StudentFavourite;
@Ignore
private boolean answerStudent = false;
public boolean isHeader() {
return answerStudent;
}
getters /setters
}
我的适配器类
public class FavourAdapter extends ArrayAdapter<Item> {
private Context context;
private ArrayList<Item> items;
private LayoutInflater vi;
public FavourAdapter(Context context, ArrayList<Item> Items) {
super(context, 0, Items);
this.context = context;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
final Item i = items.get(position);
if (i != null) {
if(i.isHeader()){
// Is si School ok in realm?
School si = (School)i;
v = vi.inflate(R.layout.row_favour_school, parent, false);
final TextView headerSection = (TextView) v.findViewById(R.id.rowFavSchoolName);
headerSection.setText(si.getSchoolName());
}else{
// Can I declare studentRow as Student in realm?
Student studentRow = (Student)i;
v = vi.inflate(R.layout.row_favour_student, parent, false);
final TextView title = (TextView)v.findViewById(R.id.rowFavStudentName);
if (title != null)
title.setText(studentRow.getStudentName());
}
}
return v;
}
我的主要 Activity 类:
public class FavourActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favour);
Realm realm = Realm.getDefaultInstance();
ArrayList<Item> items = new ArrayList<Item>();
ListView listview=null;
listview=(ListView)findViewById(R.id.favStudents_list);
//Retrieve all the school object if they have any favourite students.
RealmResults<School> schools = realm.where(School.class).equalTo("Students.StudentFavourite", true).findAll();
//Get the first favourite school
items.add(schools.get(0));
//Retrieve all the favourite student from the first favourite school.
RealmResults<Student> favStudents = schools.get(0).getStudents().where().equalTo("StudentFavourite", true).findAll();
items.add(favStudents.get(0));
items.add(favStudents.get(1));
FavourAdapter fAdapter = new FavourAdapter(this, items);
listview.setAdapter(fAdapter);
//Log.d("fav", String.valueOf(favStudents));
}
最佳答案
目前(Realm 0.87.x 及以下版本)一个 Realm 对象只能有默认的 getter 和 setter——不允许其他方法。
isHeader
是 boolean 成员 getter 的默认 Java 样式,因此 Realm 需要一个 private boolean header;
声明,这显然不存在。
A comment on this answer一位 Realm 开发人员表示,此限制可能会在 Realm 0.88 中取消 - 事实上,the pull request in question has been merged ,因此此功能可能很快就会可用!
关于java - 通过接口(interface)在Realm对象中添加方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35760858/