如何将联系人详细信息存储为类变量android

时间:2013-09-03 11:21:48

标签: android class android-contacts

这可能是一个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. 联系人的照片 - 此处不包含在代码中。

  5. 感谢您的帮助!!!

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

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

<强>输出

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

答案 2 :(得分: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;
  }
}