每一类信箱中的短信,按照日期来分隔
ListView中位置position的变化 两个Map集合 1、datePositionMap<Integer,String> key:新的ListView中的position value:该position对应的日期 2、changePositionMap<Integer,Integer> key:新的ListView中的position value:旧的ListView中的position |
1、在异步查询框架查询完毕
AsyncQueryHandler的
onQueryComplete时,将数据设置到Map中去 @Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
super.onQueryComplete(token, cookie, cursor);
if (cursor == null) {
return;
}
int count = cursor.getCount();
if (count > 0) {
int dateCount = 0;// 日期的个数
while (cursor.moveToNext()) {
int position = cursor.getPosition();
// 1、取出当前日期
long date = cursor.getLong(DATE_COLUMN_INDEX);
String dateStr = DateFormat.getDateFormat(context).format(date);
// 2、存入数据到map集合中去
if (!datePositionMap.containsValue(dateStr)) {// 如果当前datePositionMap中没有该日期,那么存入该日期
datePositionMap.put(position + dateCount, dateStr);
dateCount++;
}
// 新的位置和旧的位置的关系映射,那么存入到changePositionMap集合
changePositionMap.put(position + dateCount, position);
}
}
// 游标重新归位
cursor.move(-1);
mCursor = cursor;
folderListAdapter.changeCursor(cursor);
} 2、Cursor中重写getCount()和getView()方法@Override
public int getCount() {
return super.getCount() + datePositionMap.size();// 之前的个数+日期的个数
}原本的getView() public View getView(int position, View convertView, ViewGroup parent) {
if (!mDataValid) {
throw new IllegalStateException("this should only be called when the cursor is valid");
}
if (!mCursor.moveToPosition(position)) {
throw new IllegalStateException("couldn't move cursor to position " + position);
}
View v;
if (convertView == null) {
v = newView(mContext, mCursor, parent);
} else {
v = convertView;
}
bindView(v, mContext, mCursor);
return v;
}重写后的getView() @Override
public View getView(int position, View convertView, ViewGroup parent) {
// 这里的position是新的position
if (datePositionMap.containsKey(position)) {
// 如果datePositionMap包含position,那么该位置显示日期
TextView tvDate = (TextView) View.inflate(mContext,R.layout.folder_detail_date_item, null);
String dateStr = datePositionMap.get(position);
tvDate.setText(dateStr);
return tvDate;
} else {
// 否则的话,当前位置不在datePositionMap集合中
// if (!mCursor.moveToPosition(position)) {
// 这里的position是新的position,而这里用到的是游标position,所以要用原来的position
if (!mCursor.moveToPosition(changePositionMap.get(position))) {
throw new IllegalStateException("couldn't move cursor to position " + position);
}
View v;
if (convertView == null || (convertView.getTag() == null)) {// 前面有设置了tvDate,所以convertView不为null,还有判断是否绑定了数据,否则会报空指针异常
v = newView(mContext, mCursor, parent);
} else {
v = convertView;
}
bindView(v, mContext, mCursor);
return v;
}
}3、一些用到原来position的地方,要进行修改 ①如,listview中item的点击位置及屏蔽掉点击日期的时候出现的响应 lvFolderDetail.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (datePositionMap.containsKey(position)) {
view.setFocusable(false);
view.setClickable(false);
return;
}
// 将每条短信的id给传递过去
Cursor cursor = (Cursor) folderListAdapter.getItem(changePositionMap.get(position));
String _id = cursor.getString(ID_COLUMN_INDEX);
Intent intent = new Intent(FolderListActivity.this,SmsDetailActivity.class);
intent.putExtra("_id", _id);
FolderListActivity.this.startActivity(intent);
}
});②原有的ListView中cursor位置的判断 if (!mCursor.moveToPosition(changePositionMap.get(position))) {} 效果: |
完整代码: 文件夹列表: FolderListActivity.java
|
异常:UnsupportedOperationException 01-25 15:19:34.493: E/AndroidRuntime(1903): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView 代码: 在CursorAdapter的getView() TextView tvDate = (TextView) View.inflate(mContext,R.layout.folder_detail_date_item, null); |