我有一个片段,我在其中使用ContentObserver类,它通知我数据库表中的任何更改。
在Asynctask中,我从数据库中检索数据并将其添加到适配器。 然后使用adapter.notifyDataSetChanged()不刷新gridView,虽然在正确的调试中,我能够在arraylist中看到更新的数据,但是在UI上。
//我的适配器类
public class MyCustomAdapter extends BaseAdapter implements
StickyGridHeadersBaseAdapter {
private ArrayList<Assignment> assignmentsGlobalData;
public ArrayList<Assignment> getAssignmentsGlobalData() {
return assignmentsGlobalData;
}
private ArrayList<Section> sectionList;
private Context mContext;
public int getAssignmentsGlobalDataLength() {
return assignmentsGlobalData.size();
}
private boolean isOtherUser;
private UserObject userObj;
int selectedPosition = -1;
public void setSelected(int setPosition) {
this.selectedPosition = setPosition;
notifyDataSetChanged();
}
public MyCustomAdapter (boolean isOtherUser, UserObject userObj,
Context mContext) {
this.assignmentsGlobalData = new ArrayList<Assignment>();
this.sectionList = new ArrayList<AllAssignment.Section>();
this.isOtherUser = isOtherUser;
this.userObj = userObj;
this.mContext = mContext;
}
public void putData(ArrayList<Section> list,
ArrayList<Assignment> assignments) {
this.sectionList.clear();
this.sectionList.addAll(list);
this.assignmentsGlobalData.clear();
this.assignmentsGlobalData.addAll(assignments);
this.notifyDataSetChanged();
}
public void addData(ArrayList<Assignment> assignments,
ArrayList<Section> sectionList) {
this.assignmentsGlobalData = assignments;
this.sectionList = sectionList;
this.notifyDataSetChanged();
}
@Override
public int getCount() {
return assignmentsGlobalData.size();
}
@Override
public Object getItem(int position) {
return assignmentsGlobalData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = ((LayoutInflater) FinoitKit.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.new_assignment_item, null);
}
//I do my usual work here..
return convertView;
}
@Override
public int getCountForHeader(int header) {
if (sectionList.size() > 0)
return sectionList.get(header).num;
else
return 0;
}
@Override
public int getNumHeaders() {
return sectionList.size();
}
public void putSectionList(ArrayList<Section> list) {
this.sectionList = list;
}
@Override
public View getHeaderView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = ((LayoutInflater) FinoitKit.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.assignment_section_view, parent, false);
}
TextView date = (TextView) convertView.findViewById(R.id.header_date);
date.setText(sectionList.get(position).date);
return convertView;
}
//我的AsyncTask用于从数据库加载更新的内容
private class LoadAssignmnets extends AsyncTask<Void, Void, ArrayList<Assignment>> {
boolean isFilterChanged;
public LoadAssignmnets(boolean isFilterChanged) {
this.isFilterChanged = isFilterChanged;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
isLoading = true;
setRefreshActionButtonState(true);
if (isFilterChanged)
showAssignmentGridLoadingView();
}
@Override
protected ArrayList<Assignment> doInBackground(Void... params) {
Cursor cursor = AllAssignment.getInstance().loadAssignments(
mContext,
AllAssignment.getInstance().getStatusBundle(
AllAssignment.ALL, gradeObj, userObj, assignedBy));
ArrayList<Assignment> assignment = new ArrayList<Assignment>();
if (cursor != null && !cursor.isClosed()) {
while (cursor.moveToNext()) {
assignment.add(new Assignment(cursor));
}
cursor.close();
}
return assignment;
}
protected void onPostExecute(final ArrayList<Assignment> result) {
isLoading = false;
if (result.size() > 0) {
assignments = result;
assignmentAdaper.putData(AllAssignment.getInstance()
.getHeaderCount(result), result);
if (isFragmentAlive)
showAssignmentGridView();
} else {
showAssignmentEmptyGridView();
}
setRefreshActionButtonState(false);
};
}
提前感谢您的帮助。