androidandroid-cursoradapter

How to use CursorAdapter with better solution?


I have developed Chat UI using CursorAdapter. Almost it works well with CursorAdapter but sometimes there is exist weird results. When I do fast scrolling ListView, it caused weird issue.

Basically I tried to use getView for CursorAdapter. Below code is thing what I used.

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 way

@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 way

@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
}

For example I want to show text array like below.

A

B

C

D

E

But the result is not same with above.

wrong result 1

A

B

D

D

E

wrong result 2

A

B

empty

D

E

What is the best solution?

Tip: I must use getView() function.


Solution

  • I solved this issue by pass position instead of cursor object.

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