1

这可能是一个 JAVA 问题,但我不知道如何构建它。因此,版主请根据需要更改标签。

我正在使用以下代码检索联系人的信息...

Cursor cur = cr_RC.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));
        String name = cur
                .getString(cur
                        .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        if (Integer
                .parseInt(cur.getString(cur
                        .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            Cursor pCur = cr_RC.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                            + " = ?", new String[] { id }, null);
            while (pCur.moveToNext()) {
                // Do something with phones
            }
            pCur.close();
        }
    }
}

我想创建一个类,它包含所有这些值(多个联系人),然后检索它们以将它们存储在数据库中。但是,我不太确定如何编写该类。有人可以帮我弄这个吗?如何将这些多个联系人存储在一个类中?在存储或检索联系人信息时,如何区分一个联系人与另一个联系人?

我要存储的东西的清单..

  1. 姓名

  2. 所有电话号码

  3. 电子邮件 ID - 不包含在此处的代码中。

  4. 联系人的照片 - 不包含在此处的代码中。

谢谢你的帮助!!!

4

3 回答 3

2

这看起来像一个面向对象的编程问题。

我想您想拥有多个“联系人”,因此您可能在谈论成员变量,而不是(静态)类变量。

您需要创建一个包含存储数据所需的所有 memer 变量的类。然后编写 getter 和 setter 方法,为对象添加新数据或从对象中获取数据。

例如,您想要的课程可能如下所示:

MyContact.java

public  class MyContact {

    private String name; 
    private ArrayList<String> phoneNumbers;  // a list for storing multiple numbers
    private ArrayList<String> emailIDs;

    // storing bitmaps permanently is probably not the best solution
    private Bitmap photo; // BE VERY CAREFUL HERE, Bitmaps use lots of memory so only keep them in memory as long as needed

    public MyContact() {
        phoneNumbers = new ArrayList<String>();
        emailIDs = new ArrayList<String>();
    }

    public void setName(String name) {
        this.name = name;
    }

    public void addPhoneNumer(String number) {
         this.phoneNumbers.add(number);
    }

    public void addEmailID(String number) {
         this.emailIDs.add(number);
    }

    public String getName() {
          return name;
    }

    public String getEmaiIDByIndex(int index) {
          return emailIDs.get(index);
    }

    // and so on...
}

然后,如果你想在你的类中保存值:

MyContact c = new MyContact();

c.setName("somename");
c.addPhoneNumber("145325235235");
c.addPhoneNumber("94205325");
c.addEmailID("emailid");

// to get information and store it somewhere else:
YourDatabase.storeValue(c.getName());  // just as an example
于 2013-09-03T11:28:09.903 回答
1

这是我正在使用的完整解决方案

此方法将返回 ArrayList>,每个哈希映射包含您想要的一个联系人的完整信息。

输出

[
{phone=992-561-1618;848-807-4440;, 
contactId=1, 
photo=android.graphics.Bitmap@44f40aa0, 
address=Zalavadia Strret
Manavadar, Gujarat 362630
India, 
email=birajzalavadia@gmail.com;biraj@tasolglobal.com;, 
name=Biraj Zalavadia
}
]

public static ArrayList<HashMap<String, Object>> getContacts() {

        ArrayList<HashMap<String, Object>> contacts = new ArrayList<HashMap<String, Object>>();
        final String[] projection = new String[] { RawContacts.CONTACT_ID, RawContacts.DELETED };

        @SuppressWarnings("deprecation")
        final Cursor rawContacts = mSmartAndroidActivity.managedQuery(RawContacts.CONTENT_URI, projection, null, null, null);

        final int contactIdColumnIndex = rawContacts.getColumnIndex(RawContacts.CONTACT_ID);
        final int deletedColumnIndex = rawContacts.getColumnIndex(RawContacts.DELETED);

        if (rawContacts.moveToFirst()) {
            while (!rawContacts.isAfterLast()) {
                final int contactId = rawContacts.getInt(contactIdColumnIndex);
                final boolean deleted = (rawContacts.getInt(deletedColumnIndex) == 1);

                if (!deleted) {
                    HashMap<String, Object> contactInfo = new HashMap<String, Object>() {
                        {
                            put("contactId", "");
                            put("name", "");
                            put("email", "");
                            put("address", "");
                            put("photo", "");
                            put("phone", "");
                        }
                    };
                    contactInfo.put("contactId", "" + contactId);
                    contactInfo.put("name", getName(contactId));
                    contactInfo.put("email", getEmail(contactId));
                    contactInfo.put("photo", getPhoto(contactId) != null ? getPhoto(contactId) : "");
                    contactInfo.put("address", getAddress(contactId));
                    contactInfo.put("phone", getPhoneNumber(contactId));
                    contactInfo.put("isChecked", "false");
                    contacts.add(contactInfo);
                }
                rawContacts.moveToNext();
            }
        }

        rawContacts.close();

        return contacts;
    }

方法使用的附加方法 getContacts()

private static String getName(int contactId) {
        String name = "";
        final String[] projection = new String[] { Contacts.DISPLAY_NAME };

        final Cursor contact = mSmartAndroidActivity.managedQuery(Contacts.CONTENT_URI, projection, Contacts._ID + "=?", new String[] { String.valueOf(contactId) }, null);

        if (contact.moveToFirst()) {
            name = contact.getString(contact.getColumnIndex(Contacts.DISPLAY_NAME));
            contact.close();
        }
        contact.close();
        return name;

    }

    /**
     * This method used to get mail id from contact id.
     * 
     * @param contactId
     *            represented contact id
     * @return represented {@link String}
     */
    @SuppressWarnings("deprecation")
    private static String getEmail(int contactId) {
        String emailStr = "";
        final String[] projection = new String[] { Email.DATA, // use
                // Email.ADDRESS
                // for API-Level
                // 11+
                Email.TYPE };

        final Cursor email = mSmartAndroidActivity.managedQuery(Email.CONTENT_URI, projection, Data.CONTACT_ID + "=?", new String[] { String.valueOf(contactId) }, null);

        if (email.moveToFirst()) {
            final int contactEmailColumnIndex = email.getColumnIndex(Email.DATA);

            while (!email.isAfterLast()) {
                emailStr = emailStr + email.getString(contactEmailColumnIndex) + ";";
                email.moveToNext();
            }
        }
        email.close();
        return emailStr;

    }

    /**
     * This method used to get {@link Bitmap} From contact id.
     * 
     * @param contactId
     *            represented contact id
     * @return represented {@link Bitmap}
     */
    @SuppressWarnings("deprecation")
    private static Bitmap getPhoto(int contactId) {
        Bitmap photo = null;
        final String[] projection = new String[] { Contacts.PHOTO_ID };

        final Cursor contact = mSmartAndroidActivity.managedQuery(Contacts.CONTENT_URI, projection, Contacts._ID + "=?", new String[] { String.valueOf(contactId) }, null);

        if (contact.moveToFirst()) {
            final String photoId = contact.getString(contact.getColumnIndex(Contacts.PHOTO_ID));
            if (photoId != null) {
                photo = getBitmap(photoId);
            } else {
                photo = null;
            }
        }
        contact.close();

        return photo;
    }

    /**
     * This method used to get {@link Bitmap} From photo id.
     * 
     * @param photoId
     *            represented photo id
     * @return represented {@link Bitmap}
     */
    @SuppressWarnings("deprecation")
    private static Bitmap getBitmap(String photoId) {
        final Cursor photo = mSmartAndroidActivity.managedQuery(Data.CONTENT_URI, new String[] { Photo.PHOTO }, Data._ID + "=?", new String[] { photoId }, null);

        final Bitmap photoBitmap;
        if (photo.moveToFirst()) {
            byte[] photoBlob = photo.getBlob(photo.getColumnIndex(Photo.PHOTO));
            photoBitmap = BitmapFactory.decodeByteArray(photoBlob, 0, photoBlob.length);
        } else {
            photoBitmap = null;
        }
        photo.close();
        return photoBitmap;
    }

    /**
     * This method used to get address from contact id.
     * 
     * @param contactId
     *            represented contact id
     * @return represented {@link String}
     */
    @SuppressWarnings("deprecation")
    private static String getAddress(int contactId) {
        String postalData = "";
        String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
        String[] addrWhereParams = new String[] { String.valueOf(contactId), ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE };

        Cursor addrCur = mSmartAndroidActivity.managedQuery(ContactsContract.Data.CONTENT_URI, null, addrWhere, addrWhereParams, null);

        if (addrCur.moveToFirst()) {
            postalData = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
        }
        addrCur.close();
        return postalData;
    }

    /**
     * This method used to get phone number from contact id.
     * 
     * @param contactId
     *            represented contact id
     * @return represented {@link String}
     */
    @SuppressWarnings("deprecation")
    private static String getPhoneNumber(int contactId) {

        String phoneNumber = "";
        final String[] projection = new String[] { Phone.NUMBER, Phone.TYPE, };
        final Cursor phone = mSmartAndroidActivity.managedQuery(Phone.CONTENT_URI, projection, Data.CONTACT_ID + "=?", new String[] { String.valueOf(contactId) }, null);

        if (phone.moveToFirst()) {
            final int contactNumberColumnIndex = phone.getColumnIndex(Phone.DATA);

            while (!phone.isAfterLast()) {
                phoneNumber = phoneNumber + phone.getString(contactNumberColumnIndex) + ";";
                phone.moveToNext();
            }

        }
        phone.close();
        return phoneNumber;
    }
于 2013-09-03T11:36:12.440 回答
0

您可以创建 pojo 联系人类。姓名和号码示例:

public class Contact {
  private String name;
  private List<String> numbers;


  getName(){
    return name;
  }

  setName(String name) {
    this.name = name;
  }

  getNumbers(){
    return numbers;
  }

  setNumbers(List<String> numbers) {
    this.numbers= numbers;
  }
}
于 2013-09-03T11:34:43.687 回答