我有一个基于 android 中的 MMS 和 SMS 数据库的自定义光标适配器。代码如下所示:
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Message m = Message.getMessage(context, cursor); // gets message from cursor
int t = m.getType(); // this gets the type of message it is .. 2 = recv, 1 = sent
switch(t){
case Message.MMS_IN: // 128
return mInflater.inflate(R.layout.messagelist_item_recv, parent, false);
case Message.MMS_OUT: // 132
return mInflater.inflate(R.layout.messagelist_item_sent, parent, false);
case Message.SMS_IN: // 2
return mInflater.inflate(R.layout.messagelist_item_recv, parent, false);
case Message.SMS_OUT: // 1
return mInflater.inflate(R.layout.messagelist_item_sent, parent, false);
default:
return null;
}
}
R.layout.messagelist_item_sent
用于发送消息,用于R.layout.messagelist_item_recv
接收消息。但是查看我的消息,当列表视图正确显示时首先显示的行,但是当我将列表视图上升到新视图时,布局混淆了。recv 布局是发送的布局应该位于的位置,反之亦然。有谁知道为什么会出现这种问题?
* 编辑 **
@Override
public int getItemViewType(int position) {
// move the cursor to the position
Cursor c = (Cursor)getItem(position);
Message m = Message.getMessage(context, c);
if (isInbox(m.getType())){
inflater.inflate(recv view);
// it's been shortened
} else {
inflater.inflate(send view);
// and determine the correct type of row layout
// return 0 or 1
// use the code that you currently have from the newView method
}