2012-01-23 12 views
1

連絡先から写真を取得してリストビューに入れることができません。私はHashMapでそれを整理します。 連絡先の一部に写真がない場合は、デフォルトの写真を表示します。 今は写真がありません。コンタクト写真を取得しますか?

はここに私のコードです:あなたはそのようなSimpleAdapterを通じてImageViewsにビットマップを割り当てることはできません

cOznaci = (ImageView) findViewById(R.id.cbOznaci);  
ArrayList<HashMap<String, Object>> mapa = new ArrayList<HashMap<String, Object>>();  
ContentResolver cr = getContentResolver(); 
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 

     if(cur.getCount() > 0){ 
      while(cur.moveToNext()){ 
       String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 
       long id2 = Long.parseLong(id); 
       Object slika = loadContactPhoto(cr, id2); 
       if(Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0){ 

        final Cursor numCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null); 

        for(numCur.moveToFirst(); !numCur.isAfterLast(); numCur.moveToNext()){ 

         brTel = numCur.getString(numCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
         ime = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

         tmpIme = new String[] {ime}; 

         for(int i = 0; i < tmpIme.length; i++){ 

          HashMap<String, Object> imeMapa = new HashMap<String, Object>(); 
          imeMapa.put("imeLista", ime); 
          imeMapa.put("checkBox", slika); 
          imeMapa.put("Mobilni", brTel); 
          mapa.add(imeMapa); 
         } 

        } 
        numCur.close(); 

       } // End if 

      } // While 
     } 

     SimpleAdapter sa = new SimpleAdapter(getApplicationContext(), mapa, R.layout.imenik, new String[] {"imeLista", "checkBox", "Mobilni"}, new int[] {R.id.tvImeImenik, R.id.cbOznaci, R.id.tvSamoProba}); 
     lImenik.setAdapter(sa); 

      } 
     }); 

    } // onCreate 

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

答えて

0

、あなただけの文字列と整数を自動的に割り当てることができます。より複雑な割り当てを行うには、ViewBinderを作成する必要があります。

ただし、そのレベル14のAPIを使用して、代わりにPHOTO_URIを使用すると、ではなく、と表示されます。これは文字列であり、SimpleAdapterのデフォルトのViewBinderは適切な処理を行う必要があります。

+0

あなたは私にどのように表示できますか? – Wolf87

+0

あなたの 'while'ループでは、 '文字列photoUri = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_URI));'を介して写真のURIを取得します。 loadContactPhoto()を呼び出す必要はなく、ビットマップの代わりにSimpleAdapterに渡すことができます。 –

関連する問題