CursorAdapter Listview回收错误

时间:2015-08-30 12:04:47

标签: java android android-cursoradapter

我创建了以下CursorAdapter,它显示来自我的SQL数据库的消息,一切都添加好,直到我滚动列表,我知道对象被回收,但是以错误的方式。这是我的CursorAdapter类:

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

    }

}

不幸地,在滚动列表后,回溯后的rightDateleftDate消息日期。我认为这不是.setVisibility(View.GONE)

有任何解决此问题的建议吗?

1 个答案:

答案 0 :(得分:0)

当视图被回收时,它处于先前的状态,android没有为你清除状态。

要解决您的问题,您必须在需要时将相关视图设置为VISIBLE

编辑:

像这样,添加2行

    if (from.equals("me")) {
        // your original code

        rightDate.setVisibility(View.VISIBLE); //add this

    }

    else {

        // your original code
        leftDate.setVisibility(View.VISIBLE); //add this
    }
相关问题