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);
}
}
我有什麼改變? 謝謝!
啊,你說得對。謝謝:) – 2012-02-04 21:24:23
我經歷過同樣的事情。如果沒有contactId,則需要將其設置爲默認圖像。否則,它會使用回收視圖的圖像。 – toobsco42 2012-06-21 18:17:46