因为对于cursor流不太熟悉,特别是在数据中查询一部分属性,而在网上也没找到相应的资料,因此我花时间研究了一下。以下是研究成果:
public void getChatMsg(String friendName){ Msg msg; String sql = "select a.msg,a.username,a.sendTime,a.inOrOut from(select * from chatRecord" + " where whosRecord = ? and friend = ? order by _id desc LIMIT " +SHOW_MSG_COUNT+")a order by a._id"; Cursor cursor = db.rawQuery(sql,new String[]{XmppApplication.user,friendName}); while(cursor.moveToNext()){ msg = new Msg(cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3)); Log.i(TAG,"cursor.getString(0)="+cursor.getString(0)); //设置是和谁的聊天记录 OneFridenMessages oneFridenMessages = XmppApplication.AllFriendsMessageMapData .get(friendName); if (oneFridenMessages == null) { oneFridenMessages = new OneFridenMessages(); XmppApplication.AllFriendsMessageMapData.put(friendName,oneFridenMessages); } oneFridenMessages.MessageList.add(msg); msg = null; } cursor.close(); }cursor.getString(0)获取的是SQL查询的第一个属性,当第一个属性为a.msg时,打印结果如下:
public void getChatMsg(String friendName){ Msg msg; String sql = "select a.username,a.msg,oneFridenMessages); } oneFridenMessages.MessageList.add(msg); msg = null; } cursor.close(); }cursor.getString(0)获取的是SQL查询的第一个属性,当第一个属性为a.username时,打印结果如下:
当然cursor还有其他使用方法,但在本项目中中没用到,所以就不在此叙述了。