java - 通过接口(interface)在Realm对象中添加方法

标签 java android realm

我正在尝试通过使用查询来使用 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/

相关文章:

Java native 接口(interface) : Object Passing

java - 使用 jmap 查找当前堆大小

java - 配置 Maven 以创建包含 POM XML 中定义的所有依赖项的存档文件

java - 在泛型类中使用 equals

java - 设置为 View.VISIBLE 后,Android Button 需要点击几次才能工作

xcode - 使用 Realm.IO 在 Swift Playground 中收到 "Failed to obtain sandbox extension for path"错误

java - BestPractices : Is it bad OOP to subclass standard classes into a new one that does same thing, 但 "presuming" future 有变化吗?

java - 自动完成下拉菜单不起作用

ios - Realm 是一个什么样的数据库?

swift - 使用 2 个条件过滤 UITableView 中的 Realm 数据