Android将联系人照片添加到ListView

时间:2014-10-13 06:01:01

标签: android listview

我正在写简单的联系人阅读器。在这个简单的代码中,我有一个列表视图,我想将照片图像设置为ListView图像小部件,但我不能这样做。

我收到此错误:

Error:(47, 19) java: no suitable method found for put(java.lang.String,java.lang.String)
    method java.util.AbstractMap.put(java.lang.String,android.graphics.Bitmap) is not applicable
      (argument mismatch; java.lang.String cannot be converted to android.graphics.Bitmap)
    method java.util.HashMap.put(java.lang.String,android.graphics.Bitmap) is not applicable
      (argument mismatch; java.lang.String cannot be converted to android.graphics.Bitmap)

enter image description here

我的问题是在HashMap中设置联系人照片并在ListView中使用该问题。请帮我解决。

我的活动:

public class MyActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Cursor cursor = null;
        Context context = getBaseContext();

        String[] from = { "mphoto","id","pname" };
        int[] to = { R.id.mphoto,R.id.pid,R.id.pname};
        Bitmap bitmap = null;

        ImageView img = (ImageView) findViewById(R.id.mphoto);

        try {
            cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
            int contactIdIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID);
            int nameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            int phoneNumberIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            int photoIdIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_ID);

            cursor.moveToFirst();
            List<HashMap<String, Bitmap>> aList = new ArrayList<HashMap<String, Bitmap>>();
            do {
                HashMap<String, Bitmap> hm = new HashMap<String,Bitmap>();
                hm.put("id",    cursor.getString(nameIdx));
                hm.put("pname", cursor.getString(phoneNumberIdx));

                bitmap = loadContactPhoto(getContentResolver() , cursor.getLong(contactIdIdx));
                hm.put("mphoto", bitmap);

                aList.add(hm);
            } while (cursor.moveToNext());

            SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);
            ListView listView = ( ListView ) findViewById(R.id.listview);
            listView.setAdapter(adapter);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    public static Bitmap loadContactPhoto(ContentResolver cr, long  id) {
        Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
        InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
        if (input == null) {
            return null;
        }
        return BitmapFactory.decodeStream(input);
    }
}

我的main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical" >

    <TextView
            android:id="@+id/textview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />

    <ListView
            android:id="@+id/listview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
</LinearLayout>

我的listview_layout XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="horizontal"
        >
    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
        <ImageView
                android:id="@+id/mphoto"
                android:layout_width="75dp"
                android:layout_height="75dp"
                android:paddingTop="10dp"
                android:paddingRight="10dp"
                android:paddingBottom="10dp"
                />
        <TextView
                android:id="@+id/pid"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:textSize="15dp"
                android:layout_weight="0.33"/>

        <TextView
                android:id="@+id/pname"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:textSize="10dp"
                android:layout_weight="0.33"/>
    </LinearLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您正试图在存储hm.put("mphoto",bitmap);的{​​{1}}中存储(HashMap)位图。为位图创建一个单独的String,如

Hashmap

并使用HashMap<String,Bitmap>> aPic = new HashMap<String,Bitmap>>();

插入照片
ID