目前我正在使用CursorAdapter进行聊天应用的聊天列表视图。我从SQLite填充了最新的20个聊天记录作为我的列表,当我像其他聊天应用程序一样滚动到顶部时,我想加载早期的聊天记录。怎么做到这一点?我应该使用CursorAdapter吗?或者进行另一个查询然后在滚动到列表视图顶部时合并另一个光标?
这是我的CursorAdapter代码,请提供帮助并提出建议。
public class ChatViewAdapter extends CursorAdapter{
private static final int MSG_TIME_SHOW_DELAY = 300000;
private String mUser;
private Context mContext;
private XmppBinder mXmppBinder;
private boolean mIsMultChat;
private LayoutInflater mInflater;
private Map<String, SoftReference<XmppVcard>> mVcards;
private Map<String, SoftReference<Bitmap>> mCache;
private View.OnClickListener mResendClickListener;
private View.OnClickListener mOnImageClickListener;
private View.OnClickListener mOnAudioClickListener;
private Handler mHandler;
public ChatViewAdapter(Context context,Cursor cursor,XmppBinder xmppBinder,String currentUser){
super(context,cursor,true);
mContext = context;
mInflater = LayoutInflater.from(context);
mUser = currentUser;
mXmppBinder = xmppBinder;
mCache = new HashMap<String, SoftReference<Bitmap>>();
mVcards = new HashMap<String, SoftReference<XmppVcard>>();
}
public void setOnImageClickListener(View.OnClickListener listener){
mOnImageClickListener = listener;
}
public void setOnAudioClickListener(View.OnClickListener listener){
mOnAudioClickListener = listener;
}
private int transfer(int position) {
return getCount() - position - 1;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return super.getDropDownView(transfer(position), convertView, parent);
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
position = transfer(position);
if (position < 0 || position > getCursor().getCount() - 1) {
return null;
}
return super.getItem(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return super.getItemId(transfer(position));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return super.getView(transfer(position), convertView, parent);
}
public void setResendClickListener(View.OnClickListener listener) {
mResendClickListener = listener;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View convertView = mInflater.inflate(R.layout.list_item_chat_msg,
null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.systemTip = (TextView) convertView
.findViewById(R.id.chat_item_system_msg);
viewHolder.leftView = convertView.findViewById(R.id.chat_item_left_view);
viewHolder.leftAvartar = (HexagonImageView)convertView.findViewById(R.id.chat_item_from_avatar);
viewHolder.leftMsgTxt = (TextView)convertView.findViewById(R.id.chat_msg_from_text);
viewHolder.leftMsgImg = (AspectRatioMaskImageView)convertView.findViewById(R.id.chat_msg_from_image);
viewHolder.rightView = convertView.findViewById(R.id.chat_item_right_view);
viewHolder.rightAvartar = (HexagonImageView)convertView.findViewById(R.id.chat_item_to_avatar);
viewHolder.rightMsgTxt = (TextView)convertView.findViewById(R.id.chat_msg_to_text);
viewHolder.rightMsgImg = (AspectRatioMaskImageView)convertView.findViewById(R.id.chat_msg_to_image);
convertView.setTag(viewHolder);
return convertView;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder viewHolder = (ViewHolder)view.getTag();
if (viewHolder!=null){
final String messageOwner = cursor.getString(cursor.getColumnIndex(MsgManager.MsgColumns.SENDER));
boolean isComMsg = !messageOwner.startsWith(mUser);
long date = cursor.getLong(cursor.getColumnIndex(MsgManager.MsgColumns.CREATE_DATE));
viewHolder.systemTip.setText(DateFormat.format(
"yyyy-MM-dd hh:mm:ss", new Date(date)));
viewHolder.systemTip.setVisibility(View.GONE);
int position = cursor.getPosition();
Cursor nextCursor = (Cursor)getItem(cursor.getCount()-cursor.getPosition()-2);
if (nextCursor!=null){
long date2 = cursor.getLong(cursor
.getColumnIndex(MsgManager.MsgColumns.CREATE_DATE));
if (Math.abs(date2 - date) > MSG_TIME_SHOW_DELAY) {
viewHolder.systemTip.setVisibility(View.VISIBLE);
}
}
cursor.moveToPosition(position);
if (cursor.getPosition()==cursor.getPosition()-1){
viewHolder.systemTip.setVisibility(View.VISIBLE);
}
String content = cursor.getString(cursor.getColumnIndex(MsgManager.MsgColumns.CONTENT));
HushLog.getInstance().i("msg %s",content);
ChatMessage message= ChatMessage.fromString(content);
TextView userTextView =null;
HexagonImageView userImageView=null;
if (isComMsg){
viewHolder.rightView.setVisibility(View.GONE);
viewHolder.leftView.setVisibility(View.VISIBLE);
userImageView = viewHolder.leftAvartar;
dealMsg(cursor.getInt(0),
viewHolder.leftMsgTxt,
viewHolder.leftMsgImg,
message,
isComMsg);
} else {
viewHolder.rightView.setVisibility(View.VISIBLE);
viewHolder.leftView.setVisibility(View.GONE);
userImageView = viewHolder.rightAvartar;
dealMsg(cursor.getInt(0),
viewHolder.rightMsgTxt,
viewHolder.rightMsgImg,
message,
isComMsg);
}
final HexagonImageView imgView = userImageView;
SoftReference<XmppVcard> softReference = mVcards.get(messageOwner);
if (softReference!=null && softReference.get()!=null){
XmppVcard vcard = softReference.get();
if (vcard.getGender().equals("M")){
userImageView.setBorderColor(Color.BLUE);
}
} else {
mXmppBinder.execute(new Runnable() {
@Override
public void run() {
final XmppVcard vcard = new XmppVcard(mXmppBinder.getServiceContext());
vcard.load(mXmppBinder.getXmppConnection(),messageOwner);
mVcards.put(messageOwner, new SoftReference<XmppVcard>(vcard));
if (vcard.getGender().equals("M") && imgView!=null){
imgView.setBorderColor(Color.BLUE);
}
}
});
}
userImageView.setImageResource(R.drawable.default_avatar);
AsyncImageLoader.loadBitmapFromStore(messageOwner,new ImageLoaderListener() {
@Override
public void onImageLoaded(String url, Bitmap bitmap) {
imgView.setImageBitmap(bitmap);
}
});
}
}
private void dealMsg(int id,
TextView bodyTextView,
AspectRatioMaskImageView bodyImageView,
ChatMessage message,
boolean isComMsg){
String content = message.getBody();
ChatMessage.MsgType msgType = message.getType();
bodyTextView.setOnClickListener(null);
bodyTextView.setVisibility(View.GONE);
bodyImageView.setVisibility(View.GONE);
if (ChatMessage.MsgType.TEXT.equals(msgType)){
bodyTextView.setVisibility(View.VISIBLE);
bodyImageView.setVisibility(View.GONE);
bodyTextView.setText(content);
} else if (ChatMessage.MsgType.IMAGE.equals(msgType)){
bodyTextView.setVisibility(View.GONE);
bodyImageView.setVisibility(View.VISIBLE);
String filePath = message.getBody();
} else if (ChatMessage.MsgType.AUDIO.equals(msgType)){
bodyTextView.setVisibility(View.VISIBLE);
bodyImageView.setVisibility(View.GONE);
try {
String audioPath = message.getAudioPath();
String duration = message.getAudioDuration();
bodyTextView.setText(duration+"\"");
} catch (Exception e){
}
}
}
private class ViewHolder {
TextView systemTip;
View leftView;
HexagonImageView leftAvartar;
TextView leftMsgTxt;
AspectRatioMaskImageView leftMsgImg;
View rightView;
HexagonImageView rightAvartar;
TextView rightMsgTxt;
AspectRatioMaskImageView rightMsgImg;
}
}