我已经使用CursorAdapter开发了聊天UI。 几乎可以与CursorAdapter一起使用,但是有时会出现奇怪的结果。 当我快速滚动ListView时,它引起了奇怪的问题。
基本上,我尝试将getView用于CursorAdapter。 下面的代码是我使用的东西。
private static final String[] PROJECTION = {
Messages._ID, // 0
Messages.MESSAGEID, // 1
Messages.PHONE, // 2
Messages.BODY, // 3
Messages.INCOMING, // 4
Messages.THUMBNAIL, // 5
Messages.TYPE, // 6
Messages.TIMESTAMP, // 7
Messages.STATUS, // 8
Messages.MESSAGE_KEY, // 9
Messages.TIMEOUT, //10
Messages.MEDIA_SIZE, //11
Messages.MEDIAURL, //12
Messages.MEDIA_NAME, //13
Messages.USERID, //14
};
public static Loader<Cursor> createCursorLoader(Context context,
String chatId, int showingCount) {
Uri uri = Messages.CONTENT_URI.buildUpon().appendPath("get_messages_some").appendPath(String.format(Locale.getDefault(), "%d", showingCount)).build();
return new CursorLoader(context, uri, PROJECTION, Messages.HAS_CHATID +
" AND " + Messages.TYPE + "!=-1", new String[] { chatId }, Messages._ID +" ASC");
}
private View newView(Context context, int position) {
Cursor cursor = (Cursor) getItem(position);
if (cursor == null) {
return null;
}
boolean incoming = cursor.getInt(INCOMING) == 1;
MessageListItem item = mItemFactory.newItem(context, incoming);
return item;
}
private void bindView(View view, Context context,int position) {
final Cursor cursor = (Cursor) getItem(position);
if (cursor == null) {
return;
}
final MessageListItem item;
try {
item = (MessageListItem) view;
}
catch(RuntimeException e) {
return; //load earlier buttons
}
final String messageId = cursor.getString(MESSAGE_KEY);
item.setMessageKey(messageId);
setTextItem(item, cursor);
}
private void setTextItem(final MessageListItem item, final Cursor cursor) {
String messageId = item.getMessageKey();
if (messageId.equals(cursor.getString(MESSAGE_KEY))) {
//wrong result 1
//sometime I got wrong text from cursor.getString(BODY)
item.setText(cursor.getString(BODY));
}
else {
// here is exist issue. Why two messageId is different???
//wrong result 2
}
}
1种方式
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
v = newView(mContext, position); //custom function
} else {
v = convertView;
}
bindView(v, mContext, position); //custom function
return v;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
return null;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
}
2种方式
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
v = newView(mContext, position);
} else {
v = convertView;
}
return super.getView(position, v, parent);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
return null;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
bindView(view, context, cursor.getPosition()); //custom function
}
例如,我想显示如下文本数组。
A
B
C
D
E
但是结果和上面的不一样。
错误的结果1
A
B
D
D
E
错误的结果2
A
B
空
D
E
什么是最佳解决方案?
提示:我必须使用getView()函数。
答案 0 :(得分:0)
我通过传递位置而不是光标对象解决了这个问题。
setTextItem(item, position);
private void setTextItem(final MessageListItem item, final int position) {
Cursor cursor = (Cursor) getItem(position);
String messageId = item.getMessageKey();
if (messageId.equals(cursor.getString(MESSAGE_KEY))) {
item.setText(cursor.getString(BODY));
}
}