我正在开发一款Android应用,可以在从服务器获取数据后显示学生的成绩。退出时,应用程序会将数据保留在本地数据库中,以便在下次启动时重新加载。存储我创建的两个表,学生和课程的数据。学生课程表具有一对多的关系
我创建了一个自定义的AsyncTaskLoader来加载数据。两个类扩展了这个自定义加载器,一个名为StudentLoader,它从学生表中加载学生数据,另一个名为CourseLoader,用于从课程表中为特定学生(课程表中有学生ID外键)加载课程。
以下是自定义装载程序类
//Student Loader Class
public class StudentLoader extends DataLoader<Student> {
private String mSid;
private StudentDbAdapter dbAdapter;
public StudentLoader(Context context, String sid) {
super(context);
mSid = sid;
dbAdapter = new StudentDbAdapter(context);
}
@Override
public Student loadInBackground() {
return dbAdapter.queryStudent(mSid);
}
}
//Course Loader class
public class CoursesLoader extends DataLoader<ArrayList<Course>> {
private long mId;
private StudentDbAdapter dbAdapter;
public CoursesLoader(Context context, long id)
{
super(context);
mId = id;
dbAdapter = new StudentDbAdapter(context);
}
@Override
public ArrayList<Course> loadInBackground() {
return dbAdapter.getCourses(mId);
}
}
在启动时,我使用两个LoaderCallback类来异步(并单独)加载学生和课程数据,并根据返回的结果更新UI。我将LoaderCallback类放在显示学生成绩的片段中。
以下是LoaderCallback类
private class StudentLoaderCallbacks implements LoaderManager.LoaderCallbacks<Student>{
@Override
public Loader<Student> onCreateLoader(int i, Bundle bundle) {
return new StudentLoader(getActivity(), mId);
}
@Override
public void onLoadFinished(Loader<Student> loader, Student student) {
mStudent = student;
Log.d(TAG, "Finished Loading data...");
updateUI(mStudent);
}
@Override
public void onLoaderReset(Loader<Student> loader) {
}
}
private class CoursesLoaderCallbacks implements LoaderManager.LoaderCallbacks<ArrayList<Course>>
{
@Override
public Loader<ArrayList<Course>> onCreateLoader(int i, Bundle bundle) {
return new CoursesLoader(getActivity(), _id);
}
@Override
public void onLoadFinished(Loader<ArrayList<Course>> loader, ArrayList<Course> courses) {
mStudent.setCourses(courses);
}
@Override
public void onLoaderReset(Loader<ArrayList<Course>> loader) {
}
}
应用加载但不显示视图,只显示空白屏幕。这是updateUI方法,在加载数据后更新UI。
updateUI方法
private void updateUI(Student student)
{
if (student == null)
{
showProgressDialog(true);
return;
}
showProgressDialog(false);
ArrayList<StudentVm> studentVms = new ArrayList<>();
ArrayList<Course> courses = student.getCourses();
for (Course course : courses) {
StudentVm studentVm = new StudentVm(student.getmId(), course.getName(), Integer.valueOf(course.getCredit()).toString(), course.getGrade());
studentVms.add(studentVm);
}
Log.d(TAG, "About to display data...");
GradeListAdapter gradeAdapter = new GradeListAdapter(studentVms);
setListAdapter(gradeAdapter);
}
什么阻止片段显示数据?有谁之前经历过这个吗?怎么解决这个?
编辑1: 以下是从db:
加载学生数据的方法public Student queryStudent(String id)
{
Student stu = new Student();
Cursor cursor = db.query(DbConstants.STU_TABLE,
null, // all columns
DbConstants.STU_COLUMN_ID + "= ?", // look for a student ID
new String[]{id}, // with this value
null, // group by
null, // order by
null, // having
"1"); // limit 1 row)
cursor.moveToFirst();
int _id = 0;
while (!cursor.isAfterLast())
{
_id = cursor.getInt(cursor.getColumnIndex("_id"));
stu.setmId(cursor.getString(cursor.getColumnIndexOrThrow(DbConstants.STU_COLUMN_ID)));
stu.setFull_name(cursor.getString(cursor.getColumnIndexOrThrow(DbConstants.COLUMN_FNAME)));
stu.setAcademic_year(cursor.getInt(cursor.getColumnIndexOrThrow(DbConstants.COLUMN_ACYEAR)));
stu.setDepartment(cursor.getString(cursor.getColumnIndexOrThrow(DbConstants.COLUMN_DEPT)));
stu.setYear(cursor.getInt(cursor.getColumnIndexOrThrow(DbConstants.COLUMN_YEAR)));
}
cursor.close();
stu.set_id(_id);
return stu;
}
即使我在模拟器中关闭应用程序,logcat也会一直显示以下消息。
答案 0 :(得分:0)
再次分析代码后,我发现错误是一个相当简单的错误,即我忘记在加载学生数据的while循环中添加cursor.moveToNext()
。这导致了一个无限循环,阻止了应用程序加载数据。