我在MainActivity中使用AsyncTask从DB读取所有数据,并使用静态SparseArray来保存这些数据,在getView中我立即使用SparseArray。但它仍然不顺利。 AsyncTask
@Override
protected Void doInBackground(Integer... position) {
for (int i = 0; i < position[0]; i++) {
int dayOfWeek = i % 8 - 1;
int onClass = i / 8 + 1;
Item item = new Item();
if (i % 8 != 0 && TimeUtil.judgeTime(context, dayOfWeek, onClass)) {
item.setColor(1);
}
if (i % 8 != 0 && TimeUtil.hasSchool(context, dayOfWeek, onClass)) {
Curriculum curriculum = Curriculum.getCurriculum(context, dayOfWeek, onClass);
courseId = curriculum.getCourseId();
buildId = curriculum.getBuildingId();
roomNum = curriculum.getRoomNum();
String nickName = getCourseInfo(context, courseId);
String buildName = getBuildInfo(context, buildId);
item.setNickName(nickName);
item.setPlace(buildName);
item.setRoomNum(roomNum);
item.setHasCourse(1);
}
itemCache.append(i, item);
}
return null;
}
和getView
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(context).inflate(R.layout.gridview_item, null);
courseText = (TextView) convertView.findViewById(R.id.grid_item_nick);
placeText = (TextView) convertView.findViewById(R.id.grid_item_palce);
roomText = (TextView) convertView.findViewById(R.id.grid_item_room);
if (position % 8 == 0) {
convertView.setBackgroundColor(getResources().getColor(R.color.gray));
courseText.setText("第" + onClass + "节");
return convertView;
}
ItemLoadAsyncTask.Item item = ItemLoadAsyncTask.itemCache.get(position);
if (item != null && item.getColor() == 1) {
convertView.setBackgroundColor(Color.WHITE);
}
if (item.getHasCourse() == 1) {
setInfoView(item);
}
return convertView;
}