android - ListView 中的重复项(图片)

标签 android

我正在构建类似短信的小型应用程序。我制作了对话 ListView ,但是有问题。列表中的每一行都有照片(联系人照片),如果我向下滚动,某些行会有来自不同行的照片(从列表的开头)。

这是我的适配器:

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

}

我的 Activity 显示了列表:

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

我需要改变什么? 谢谢!

最佳答案

问题在这里:

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 将保留您之前设置的旧图像。

关于android - ListView 中的重复项(图片),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9144349/

相关文章:

android - Wi-Fi P2P。通知所有同龄人某件事

java - 安卓 : How to create image like an avatar contact?

Android 无法将数据库复制到 Assets 文件夹

android - 如何在我的项目中使用 Android Wheel?

android - 由于错误,无法在Firestore中更新阵列

java - java(Android)中通过日历函数获取负值

Android 模型 - 用于游戏开发的 ARMv6 和 ARMv7 设备?

android - 如何显示通过服务器发送的 FCM Firebase 通知

android - 如何在 ExpandableListView 中保留滚动位置

Android 应用程序 ClassNotFoundException,无法实例化应用程序