我正在尝试使用标题制作带有标题的列表视图,方法是使用查询来检索最喜欢的学校及其最喜欢的学生使用Realm。
点灰色中学
肯特
简
我的问题是我是否可以在领域对象中实现方法,以便我可以使用该方法来查明领域结果是否为标题或项目,如果无法完成,我如何绕过并仍然打印标题以及来自领域结果的列表视图。
编译以下代码时也出错了。它说“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;
}
}
我的主要活动课程:
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));
}
答案 0 :(得分:2)
目前(Realm 0.87.x及以下版本)Realm对象只能 具有默认的getter和setter - 不允许其他方法。
isHeader
是布尔成员的getter的默认Java样式,因此Realm期待private boolean header;
声明,显然不存在。
A comment on this answer表示可以在Realm 0.88中取消这个限制 - 实际上是the pull request in question has been merged,所以这个功能很快就会推出!