如何使用 CursorAdapter 有更好的解决方案?

How to use CursorAdapter with better solution?

我使用 CursorAdapter 开发了 Chat 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
}

例如,我想显示如下所示的文本数组。

一个

B

C

D

E

但是结果和上面的不一样

错误结果 1

一个

B

D

D

E

错误结果 2

一个

B

D

E

最好的解决方案是什么?

提示:我必须使用 getView() 函数。

我通过传递位置而不是光标对象解决了这个问题。

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));
    }    
}