显示来自电话簿的所选用户图像

时间:2014-02-25 06:29:32

标签: android xml listview

我正在创建一个多用户聊天应用程序。收件箱活动从我用于存储的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();
    }
    */
}

2 个答案:

答案 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;
}

答案 1 :(得分:0)

我从您的代码中了解到,您需要为显示联系人而夸大自定义布局。 我相信你的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
}