2012-02-04 70 views
1

我建设小短信一样的应用。我做了会话列表视图,但有一个问题。列表中的每一行都有照片(联系人照片),如果我向下滚动某些行,则会从不同行(来自列表的开始)中获取照片。重复项目(照片)

这里是我的适配器:

import android.content.ContentResolver; 
import android.content.Context; 
import android.database.Cursor; 
import android.graphics.Bitmap; 
import android.text.format.DateUtils; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.SimpleCursorAdapter; 
import android.widget.TextView; 

public class ConversationAdapter extends SimpleCursorAdapter { 
    private final String TAG = "ConversationAdapter"; 
    static final String[] FROM = {"body", "address", "date", "m_size"}; 
    static final int[] TO = {R.id.textMsg, R.id.textPerson, R.id.textDate, R.id.textConvCounter}; 
    ImageView imageAvatar; 
    ContentResolver contentResolver; 
    LayoutInflater layoutInflater; 

    public ConversationAdapter(Context context, Cursor c) { 
     super(context, R.layout.row, c, FROM, TO); 
    } 
    @Override 
    public void bindView(View row, Context context, Cursor cursor) { 
     super.bindView(row, context, cursor); 

     long timestamp = cursor.getLong(cursor.getColumnIndex("date")); 
     TextView textDate = (TextView) row.findViewById(R.id.textDate); 
     textDate.setText(DateUtils.getRelativeTimeSpanString(timestamp)); 
     TextView textMsg = (TextView) row.findViewById(R.id.textMsg); 

     String previewMsg = cursor.getString(cursor.getColumnIndex("body")); 
     if (previewMsg.length() > 40) 
     { 
      textMsg.setText(previewMsg.substring(0, 37) + "..."); 
     } 

     TextView textPerson = (TextView) row.findViewById(R.id.textPerson); 
     String contactId = DataManager.getContactId(context, (textPerson.getText()).toString()); 
     if (contactId != "") 
     { 
      contentResolver = context.getContentResolver(); 
      imageAvatar = (ImageView) row.findViewById(R.id.imageAvatar); 
      Long lContactId = Long.parseLong(contactId); 
      Bitmap bitmap = DataManager.getContactPhoto(contentResolver, lContactId); 
      if (bitmap != null) 
      { 
       imageAvatar.setImageBitmap(bitmap); 
       bitmap = null; 
      } 
      String contactName = DataManager.getContactName(context, (textPerson.getText()).toString()); 
      textPerson.setText(contactName); 
     } 
    } 

} 

,并有我的活动,显示列表:

import android.app.Activity; 
import android.content.ContentResolver; 
import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListView; 

public class ConversationsActivity extends Activity { 
    private final String TAG = "ConversationsActivity"; 
    ConversationAdapter adapter; 
    ContentResolver contentResolver; 
    Cursor cursor; 
    ListView convList; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.conversations_list); 
     convList = (ListView) findViewById(R.id.convList); 

    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     setupConvsList(); 
    } 
    void setupConvsList() 
    { 
     Context context = getApplicationContext(); 
     contentResolver = context.getContentResolver(); 
     cursor = contentResolver.query(Uri.parse("content://mms-sms/conversations"), null, null, null, "date DESC"); 
     startManagingCursor(cursor); 

     adapter = new ConversationAdapter(this, cursor); 
     convList.setAdapter(adapter); 
    } 
} 

我有什么改变? 谢谢!

回答

2

的问题是在这里:

if (contactId != "") 
     { 
      contentResolver = context.getContentResolver(); 
      imageAvatar = (ImageView) row.findViewById(R.id.imageAvatar); 
      Long lContactId = Long.parseLong(contactId); 
      Bitmap bitmap = DataManager.getContactPhoto(contentResolver, lContactId); 
      if (bitmap != null) 
      { 
       imageAvatar.setImageBitmap(bitmap); 
       bitmap = null; 
      } 
      String contactName = DataManager.getContactName(context, (textPerson.getText()).toString()); 
      textPerson.setText(contactName); 
     } 

你必须做一个else声明过,并设置一个默认的图像或东西有..否则你可重复使用的View将继续与您之前设置的旧形象。

+0

啊,你说得对。谢谢:) – 2012-02-04 21:24:23

+0

我经历过同样的事情。如果没有contactId,则需要将其设置为默认图像。否则,它会使用回收视图的图像。 – toobsco42 2012-06-21 18:17:46