以下是我的学生适配器类,用于显示学生,它有一个切换按钮,可以将学生设置为最喜欢的学生。
当我将学生设置为最喜欢的(真实)时,应用程序崩溃了。
我的问题是如何更新realm文件中的数据,以便将喜爱的学生设置为true。
我的第二个问题是我可以在学生活动而不是学生适配器中这样做吗?我觉得这不对,但切换按钮位于视图中。
学生列表视图的学生适配器:
public class StudentAdapter extends RealmBaseAdapter<Student> implements ListAdapter {
....
realm = Realm.getDefaultInstance();
....
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.row_student, parent, false);
viewHolder = new ViewHolder();
viewHolder.studentID = (TextView) convertView.findViewById(R.id.rowStudentID);
viewHolder.toggleBtn = (ToggleButton) convertView.findViewById(R.id.toggleStar);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final Student item = realmResults.get(position);
Boolean checkStar = item.getStudentFavourite();
if (checkStar.equals(false)) {
viewHolder.toggleBtn.setChecked(false);
} else {
viewHolder.toggleBtn.setChecked(true);
}
viewHolder.toggleBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (viewHolder.toggleBtn.isChecked()) {
Student student = realm.where(Student.class).equalTo("StudentID", item.getStudentID()).findFirst();
Log.d("toggle", String.valueOf(student));
realm.beginTransaction();
student.setStudentFavourite(true);
realm.commitTransaction();
....
return convertView;
....
学生对象
public class Student extends RealmObject{
@Required
private String StudentID;
private Boolean StudentFavourite;
public String getStudentID() {
return StudentID;
}
public void setStudentID(String studentID) {
StudentID = studentID;
}
public Boolean getStudentFavourite() {
return StudentFavourite;
}
public void setStudentFavourite(Boolean studentFavourite) {
StudentFavourite = studentFavourite;
}
}
EXCEPTION: main java.lang.NullPointerException at
com.example.kent.schooldirectory.StudentAdapter$1.onCheckedChanged(StudentAdapter.java:85)
at android.widget.CompoundButton.setChecked(CompoundButton.java:126)
at android.widget.ToggleButton.setChecked(ToggleButton.java:73)
at com.example.kent.schooldirectory.StudentAdapter.getView(StudentAdapter.java:62)
at android.widget.AbsListView.obtainView(AbsListView.java:2251)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
答案 0 :(得分:0)
感谢您的回复。我设法找到答案。
如下:
viewHolder.toggleBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (viewHolder.toggleBtn.isChecked()) {
String test1 = item.getStudentID();
Student favStudent = realm.where(Student.class).equalTo("StudentID", test1).findFirst();
realm.beginTransaction();
favStudent.setStudentFavourite(true);
realm.commitTransaction();
viewHolder.toggleBtn.setChecked(true);
//Log.d("toggle", test1);
} else {
String test2 = item.getStudentID();
Student notfavStudent = realm.where(Student.class).equalTo("StudentID", test2).findFirst();
realm.beginTransaction();
notfavStudent.setStudentFavourite(false);
realm.commitTransaction();
viewHolder.toggleBtn.setChecked(false);
//Log.d("toggle", test2);
}
}