2015-08-30 2 views
0

Я создал следующий CursorAdapter, который показывает сообщения из моей базы данных SQL, все добавлено хорошо, пока я не прокручу список, я знаю, что объекты переработаны, но неправильно. Вот мой CursorAdapter класс:CursorAdapter Listview recycling wrong

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, после прокрутки списка, датируется rightDate и leftDate пропадает после переезда обратно. Я думаю, что это не связано с .setVisibility(View.GONE)

Любые предложения по исправлению этого вопроса?

ответ

0

, когда представление переработано, оно находится в предыдущем состоянии, android не очистил статус для вас.

Чтобы устранить проблему, вы должны установить вид в вопросе VISIBLE при необходимости

Edit:

как это, добавьте 2 строки

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

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

    } 

    else { 

     // your original code 
     leftDate.setVisibility(View.VISIBLE); //add this 
    } 
+0

Это работает! Большое вам спасибо :) Я отвечу через несколько минут. –

Смежные вопросы