Search code examples
javaandroidandroid-cursoradapter

CursorAdapter Listview recycling wrong


I created the following CursorAdapter which shows messages from my SQL database, everything is added well until I scroll the list, I know that the objects are recycled, but in a wrong way. Here is my CursorAdapter class:

    public class ChatAdapter extends CursorAdapter {

    public ChatAdapter(Context context, Cursor cursor, int flags) {
        super(context, cursor, 0);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.chat_item, parent,
                false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // Find fields to populate in inflated template
        TextView left = (TextView) view.findViewById(R.id.lefttext);
        TextView right = (TextView) view.findViewById(R.id.righttext);
        LinearLayout rightBubble = (LinearLayout) view
                .findViewById(R.id.right_bubble);
        LinearLayout leftBubble = (LinearLayout) view
                .findViewById(R.id.left_bubble);
        TextView leftDate = (TextView) view.findViewById(R.id.leftdate);
        TextView rightDate = (TextView) view.findViewById(R.id.rightdate);
        // Extract properties from cursor
        String from = cursor.getString(cursor.getColumnIndexOrThrow("from"));
        String txt = cursor.getString(cursor.getColumnIndexOrThrow("message"));
        String date = cursor.getString(cursor.getColumnIndexOrThrow("t"));
        String id = cursor.getString(cursor.getColumnIndexOrThrow("id"));
        // Parse time
        long datevalue = Long.valueOf(date) * 1000;
        Date dateformat = new java.util.Date(datevalue);
        String convert = new SimpleDateFormat("HH:mm").format(dateformat);

        // Populate fields with extracted properties
        if (from.equals("me")) {

            right.setText(txt);
            left.setText("");
            rightBubble
                    .setBackgroundResource(R.drawable.balloon_outgoing_normal);
            leftBubble.setBackgroundDrawable(null);
            rightDate.setText(convert);
            leftDate.setVisibility(View.GONE);

        }

        else {

            left.setText(txt);
            right.setText("");
            leftBubble
                    .setBackgroundResource(R.drawable.balloon_incoming_normal);
            rightBubble.setBackgroundDrawable(null);
            leftDate.setText(convert);
            rightDate.setVisibility(View.GONE);
        }

    }

}

Unfortenately, after scrolling the list, dates from the rightDate and leftDate dissapears after moving back. I think it't due the .setVisibility(View.GONE)

Any suggestions to fix this?


Solution

  • when the view is recycled, it is in the previous state, android did not clear the status for you.

    To fix your problem, you have to set the view in question to VISIBLE when needed

    Edit:

    like this, add the 2 lines

        if (from.equals("me")) {
            // your original code
    
            rightDate.setVisibility(View.VISIBLE); //add this
    
        }
    
        else {
    
            // your original code
            leftDate.setVisibility(View.VISIBLE); //add this
        }