2014-02-25 45 views
2

我正在創建一個多用戶聊天應用程序。收件箱活動從用於存儲的XML加載用戶列表。我有一個問題,我希望當收件箱活動加載用戶列表時,它還檢查這些用戶是否存在於電話簿中。如果任何用戶在電話簿中,那麼它將在該列表視圖中加載該用戶的圖像。 我在某種程度上解決了我的問題,但我無法顯示選定的用戶圖像。請給我一些提示,告訴你如何做到這一點。顯示電話簿中選定的用戶圖像

這裏是收件箱中的快照

snapshot

收件箱活動的java:

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.users_index); 
     listView=(ListView)findViewById(R.id.listview); 
     actionBar = getActionBar(); 
     actionBar.setTitle("Inbox Messages"); 
     //getNumber(this.getContentResolver()); 
     getdata(); 
     listView.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       TextView uname=(TextView)view.findViewById(R.id.sendername); 
       username = uname.getText().toString(); 
       Intent i = new Intent(getApplicationContext(), MainActivity.class); 
       i.putExtra("username", username); 
       startActivity(i); 
      } 
     }); 
    } 
    private void getdata() { 
     try { 
      File fXmlFile = new File("/data/data/net.multiplesystem.nosms/Messeges/UserList.xml"); 
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
      Document doc = dBuilder.parse(fXmlFile); 
      doc.getDocumentElement().normalize(); 
      NodeList nList = doc.getElementsByTagName("details"); 
      for (int temp = 0; temp < nList.getLength(); temp++) { 
       Node nNode = nList.item(temp); 
       if (nNode.getNodeType() == Node.ELEMENT_NODE) { 
        Element eElement = (Element) nNode; 
        String a=eElement.getElementsByTagName("name").item(0).getTextContent().toString(); 
        userIndex.add(a); 
       } 
       } 
     } catch (ParserConfigurationException e) { 
      e.printStackTrace(); 
     } catch (SAXException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     setListAdapter(); 
    } 

    private void setListAdapter(){ 
     adapter = new ArrayAdapter<String>(this,R.layout.user_index_layout,R.id.sendername,userIndex); 
     listView.setAdapter(adapter); 
     registerForContextMenu(listView); 
    } 

    /* 
    public void getNumber(ContentResolver cr) 
    { 
     Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); 
     while (phones.moveToNext()) 
     { 
      name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
      phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)); 
      contactId = Long.parseLong(id); 
      System.out.println(".................."+phoneNumber); 
      System.out.println(".................."+name); 
      Number.add(phoneNumber); 

     } 
     phones.close();// close cursor 
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.user_index_layout,R.id.sendername,Number); 
     listView.setAdapter(adapter); 
     setListAdapter(); 
    } 
    */ 
} 
+0

有很多答案是請解釋如何使用ListView。請瀏覽網站,你會發現你需要的東西。 –

+0

我有很多搜索,但找不到所需的。 :( – MSJohn

+0

也許你可以發佈一些縮小問題的代碼,然後如果已發佈的響應不能回答你的問題=) –

回答

0

試試這個,它可以幫助,通過電話號碼,以達到照片

public Bitmap getPhoto(String phoneNumber) { 
    Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); 
    Uri photoUri = null; 
    ContentResolver cr = this.getContentResolver(); 
    Cursor contact = cr.query(phoneUri, 
      new String[] { ContactsContract.Contacts._ID }, null, null, null); 

    if (contact.moveToFirst()) { 
     long userId = contact.getLong(contact.getColumnIndex(ContactsContract.Contacts._ID)); 
     photoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, userId); 

    } 
    else { 
     Bitmap defaultPhoto = BitmapFactory.decodeResource(getResources(), android.R.drawable.ic_menu_report_image); 
     return defaultPhoto; 
    } 
    if (photoUri != null) { 
     InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(
       cr, photoUri); 
     if (input != null) { 
      return BitmapFactory.decodeStream(input); 
     } 
    } else { 
     Bitmap defaultPhoto = BitmapFactory.decodeResource(getResources(), android.R.drawable.ic_menu_report_image); 
     return defaultPhoto; 
    } 
    Bitmap defaultPhoto = BitmapFactory.decodeResource(getResources(), android.R.drawable.ic_menu_report_image); 
    return defaultPhoto; 
} 
+0

如何將位圖對象傳遞給適配器? – MSJohn

+0

hey在適配器中調用它的方法它自己...在設置位圖到imageview的時候做到這一點.. imageView.setImageBitmap(getPhoto(999999999)); –

+0

感謝您的回答,我會試試:) – MSJohn

0

我從你的代碼中瞭解到,你需要膨脹自定義layo用於顯示聯繫人。 我相信你R.layout.user_index_layout是類似的東西:

<LinearLayout 
    android:orientation="horizontal" 
    ... > 
    <ImageView 
     android:id="@+id/userIndexLayout_img" 
     ... /> 
    <TextView 
     ... /> 
</LinearLayout> 

所以,想象一些回調,當用戶將成爲連接像這樣得到通知(多個僞代碼):

public void onUserConnected(int id) { 
    int indexInsideListView = getUserIndex(id); 
    View userView = listView.getChildAt(indexInsideListView); 
    ImageView imgView = (ImageView) userView.findViewById(R.id.userIndexLayout_img); 
    // User ImageView.setImageResource/Drawable/Bitmap 
}