Reputation: 397
I am trying to make a listview with headers as follow by using queries to retrieve the favourite school and its favourite students using Realm.
Point Grey Secondary
Kent
Jane
My question is whether I could implement methods in realm objects so I can use the method to find out if the realm result is header or items, if it couldn't be done, how I can get around and still print the header and the listview from realm result.
I also have an error when it compile the following code. It says "getters isHeader is not associated with any field"
My interface Class:
public interface Item {
boolean isHeader();
}
My models class:
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
}
My Adapter Class
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;
}
}
My main activity class:
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));
}
Upvotes: 1
Views: 402
Reputation: 16394
Currently (Realm 0.87.x and below) a Realm object can only have default getters and setters - no other methods are allowed.
isHeader
is default Java style for the getter for a boolean member, so Realm is expecting a private boolean header;
declaration, which obviously doesn't exist.
A comment on this answer from one of the Realm developers indicates that this restriction may be lifted in Realm 0.88 - in fact, the pull request in question has been merged, so this feature will probably be available soon!
Upvotes: 2