【问题标题】:Display selected users image from phonebook显示电话簿中选定的用户图像
【发布时间】:2014-02-25 06:29:32
【问题描述】:

我正在创建一个多用户聊天应用程序。收件箱活动从我用于存储的 XML 中加载用户列表。我有一个问题,我希望当收件箱活动加载用户列表时,它还检查这些用户中的任何一个是否存在于电话簿中。如果有任何用户在电话簿中,那么它会在列表视图中加载该用户的图像。 我在一定程度上解决了我的问题,但我无法显示选定的用户图像。请给我一些提示如何做到这一点。提前谢谢你。

这是收件箱的快照

收件箱活动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();
    }
    */
}

【问题讨论】:

  • 有很多答案解释了如何根据您的要求使用 ListView。请浏览该网站,您一定会找到您需要的。
  • 我搜索了很多,但找不到想要的。 :(
  • 也许您可以发布一些代码来缩小问题范围,如果发布的回复没有回答您的问题 =)
  • 抱歉回复晚了,我已经贴出代码请查收。

标签: android xml listview


【解决方案1】:

试试这个可能有帮助,通过电话号码获取照片

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

【讨论】:

  • 如何将位图对象传递给适配器?
  • 嘿,它自己在适配器中调用该方法...在将位图设置为 imageview 时执行此操作.. imageView.setImageBitmap(getPhoto(999999999));
  • 谢谢你的回答,我试试看:)
  • 我已经发布了我的代码,请检查并提出一些建议。
  • 而不是使用ArrayAdapter,使用自定义适配器并创建一个可以存储名称和对象的模型对象,将此对象传递给适配器。在设置数据时,您可以设置所有参数&也设置图像视图
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-28
    • 2015-05-20
    • 2012-08-20
    • 1970-01-01
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    相关资源
    最近更新 更多