1

我想在列表视图中显示所有联系人详细信息(如显示标题名称、电话号码和图像) ,他们在我的移动联系人列表中有手机号码。我已经得到标题名称,电话号码。我按照这个线程来实现列表中的照片。但没有成功。

要获取显示标题名称,电话号码,我使用以下方法。尝试使用相同查询获取图像时出现错误。我可以在一个查询中获得所有信息,还是需要两个查询?

public static ArrayList<Person> getAllContactsWithPhoto(Context context) {

    ArrayList<Person> allContacts = new ArrayList<Person>();
    ContentResolver cr = context.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));

            if (Integer
                    .parseInt(cur.getString(cur
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

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

                while (pCur.moveToNext()) {

                    // fill object data 
                    Person aPerson = new Person();
                    aPerson.setName(pCur.getString(pCur
                            .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));
                    aPerson.setNumber(pCur.getString(pCur
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));

                    // getting error in log cat.
                    Uri my_contact_Uri = Uri.withAppendedPath(
                            ContactsContract.Contacts.CONTENT_URI,
                            String.valueOf(id));

                    aPerson.setImage_Uri(my_contact_Uri);

                    Log.d(" Number List",
                            " " + aPerson.getName() + " "
                                    + aPerson.getNumber() + " "
                                    + aPerson.getImage_Uri());

                    allContacts.add(aPerson);
                }
                pCur.close();
            }
        }
    }
    return allContacts;
}

我自定义的适配器ContactListAdapter getView() 就像:

   @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            holder = new ViewHolder();

            convertView = inflater.inflate(R.layout.list_row, null);
            convertView.setMinimumHeight(50);

            holder.textViewContactName = (TextView) convertView
                    .findViewById(R.id.textview_contact_name);
            holder.textView_Contact_Number = (TextView) convertView
                    .findViewById(R.id.textview_number);
            holder.imgViewContactImage = (ImageView) convertView
                    .findViewById(R.id.imgViewContactImage);

            convertView.setTag(holder);

        } else
            holder = (ViewHolder) convertView.getTag();

        if (allContactsNumbers.size() <= 0) {
            holder.textViewContactName.setText("No Data");

        } else {
            holder = (ViewHolder) convertView.getTag();

            String name = allContactsNumbers.get(position).getName();
            holder.textViewContactName.setText(name);

            String number = allContactsNumbers.get(position).getNumber();
            holder.textView_Contact_Number.setText(number);

            Uri Uri = allContactsNumbers.get(position).getImage_Uri();
            if (Uri != null) {
                holder.imgViewContactImage.setImageURI(Uri);
            } else {
                holder.imgViewContactImage.setImageResource(R.id.useLogo);
            }

            Log.d("Contacts in Adapter", "" + name + "" + number);

        }
        return convertView;
    }

我的个人类:

  public class Person {
      // has setter & getter
    private String name;
    private String phone_number;
    private Uri image_uri; }

有人可以帮我解决这个问题吗?

4

1 回答 1

0

要获取图像 Uri,请尝试以下操作:

Uri my_contact_Uri = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));  

如果setImageURI(my_contact_Uri)不工作试试这个:

Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),Uri.parse(my_contact_Uri));
于 2013-10-19T19:10:16.757 回答