Java 通过接口在领域对象中添加方法
我试图创建一个带有标题的列表视图,如下所示,通过使用领域查询来检索最喜欢的学校及其最喜欢的学生 点灰二级Java 通过接口在领域对象中添加方法,java,android,realm,Java,Android,Realm,我试图创建一个带有标题的列表视图,如下所示,通过使用领域查询来检索最喜欢的学校及其最喜欢的学生 点灰二级 肯特 简 我的问题是,我是否可以在领域对象中实现方法,这样我就可以使用该方法来确定领域结果是标题还是项目,如果无法实现,我如何能够绕过并仍然从领域结果打印标题和列表视图 我在编译以下代码时也有一个错误。它说“getters isHeader不与任何字段关联” 我的接口类: public interface Item { boolean isHeader(); } 我的模特课: pub
肯特
简
我的问题是,我是否可以在领域对象中实现方法,这样我就可以使用该方法来确定领域结果是标题还是项目,如果无法实现,我如何能够绕过并仍然从领域结果打印标题和列表视图 我在编译以下代码时也有一个错误。它说“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
}
公立学校扩展RealmObject实现项{
@必需的
私人字符串学校ID;
私有字符串学名;
私人地产商学生;
@忽略
私有布尔回答学校=真;
公共布尔isHeader(){
返回学校;
}
能手/二传手
}
公共类学生扩展RealmObject实现项{
@必需的
私人字符串学生ID;
私有字符串StudentName;
私人布尔型学生最喜欢的;
@忽略
私有布尔应答student=false;
公共布尔isHeader(){
回答学生;
}
能手/二传手
}
我的适配器类
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;
}
公共类FavorAdapter扩展了ArrayAdapter{
私人语境;
私有ArrayList项;
私人停车场6号;
公共收藏夹适配器(上下文、ArrayList项){
超级(上下文,0,项);
this.context=上下文;
vi=(LayoutInflater)context.getSystemService(context.LAYOUT\u充气机\u服务);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
最终项目i=项目。获取(位置);
如果(i!=null){
如果(i.isHeader()){
//学校在这个领域可以吗?
学校si=(学校)i;
v=vi.充气(R.布局.行\学校,家长,假);
最终文本视图标题节=(文本视图)v.findviewbyd(R.id.rowFavSchoolName);
headerSection.setText(si.getSchoolName());
}否则{
//我可以在域中将studentRow声明为Student吗?
学生行=(学生)i;
v=vi.充气(R.布局.行\赞成\学生,家长,假);
最终文本视图标题=(文本视图)v.findviewbyd(R.id.rowFavStudentName);
如果(标题!=null)
title.setText(studentRow.getStudentName());
}
}
返回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));
}
公共类FavorActivity扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_);
Realm Realm=Realm.getDefaultInstance();
ArrayList items=新建ArrayList();
ListView ListView=null;
listview=(listview)findViewById(R.id.favstudens_list);
//如果他们有最喜欢的学生,则检索所有学校对象。
RealmResults schools=realm.where(School.class).equalTo(“Students.studentfavorite”,true).findAll();
//得到第一个最喜欢的学校
items.add(schools.get(0));
//从第一个喜爱的学校检索所有喜爱的学生。
RealmResults favstudens=schools.get(0).getStudents().where().equalTo(“studentfavorite”,true).findAll();
items.add(favstudens.get(0));
items.add(favstudens.get(1));
FavorAdapter fAdapter=新的FavorAdapter(此,项);
setAdapter(fAdapter);
//Log.d(“fav”,String.valueOf(favstudens));
}
当前(领域0.87.x及以下)领域对象只能有默认的getter和setter,不允许使用其他方法
isHeader
是布尔成员getter的默认Java样式,因此Realm需要一个private boolean头;
声明,该声明显然不存在
来自某个领域的开发者表示,这个限制可能会在领域0.88中解除——事实上,所以这个功能可能很快就会可用