2

我已经建立了一个安卓应用程序。它将从(我的手机的)默认通讯录中获取所有联系人(1000 个联系人)。然后,我将它们全部显示在(我的应用程序的)列表视图中。但是,我花了大约 13 秒来加载和显示在列表视图中。在下面的代码中,我使用了 3 个命令来查询:每个联系人的姓名、电话号码和公司。我认为这就是我的应用程序花费太多时间在列表视图上加载和显示数据的原因。

我有两个问题:

  1. 如何仅使用 1 个 SQLite 命令获取 android 中联系人的所有信息?
  2. 有什么方法可以更快地将数据加载和显示到列表视图上???

这是要查询的代码:联系人的姓名、电话号码、公司。查询的结果将存储到游标中。然后我用光标设置listAdapter。

public void addNewContacts(String name, String phone, String com){
    String DisplayName = name;
     String MobileNumber = phone;
     String company = com;
     String jobTitle = "Engineer";

     ArrayList < ContentProviderOperation > ops = new ArrayList < ContentProviderOperation > ();

     ops.add(ContentProviderOperation.newInsert(
     ContactsContract.RawContacts.CONTENT_URI)
         .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
         .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
         .build());

     //------------------------------------------------------ Names
     if (DisplayName != null) {
         ops.add(ContentProviderOperation.newInsert(
         ContactsContract.Data.CONTENT_URI)
             .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
             .withValue(ContactsContract.Data.MIMETYPE,
         ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
             .withValue(
         ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
         DisplayName).build());
     }

     //------------------------------------------------------ Mobile Number                     
     if (MobileNumber != null) {
         ops.add(ContentProviderOperation.
         newInsert(ContactsContract.Data.CONTENT_URI)
             .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
             .withValue(ContactsContract.Data.MIMETYPE,
         ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
             .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, MobileNumber)
             .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
         ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
             .build());
     }

     //------------------------------------------------------ Organization
     if (!company.equals("") && !jobTitle.equals("")) {
         ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
             .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
             .withValue(ContactsContract.Data.MIMETYPE,
         ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
             .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, company)
             .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
             .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, jobTitle)
             .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
             .build());
     }

     // Asking the Contact provider to create a new contact                 
     try {
         getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
     } catch (Exception e) {
         Log.e("ERROR: ", e.getMessage());
     } 
}
4

1 回答 1

1

我不认为你可以,或者它应该是复杂的。

在获取任何信息之前,我需要验证 Name 是否至少存在。

这是我使用的代码:

 public class ContactListControl {

/** be sure that final fields equal to input_display_name */
private final static String NAME = "name";
private final static String GMAIL = "gmail";
private final static String PHONE = "phone";

private ContactListControl(){}

public static void onGetContactInfo(Intent data, Context context, List<InputView> inputViewList) {      

    Map<String, String> contactDataMap = new HashMap<String, String>();

    ContentResolver cr = context.getContentResolver();

    Uri contactData = data.getData();
    //Cursor cursor =  managedQuery(contactData, null, null, null, null);
    Cursor cursor =  cr.query(contactData, null, null, null, null);
    cursor.moveToFirst();
    String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
    String id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

    contactDataMap.put(NAME, (name != null)?name:"");


    if (Integer.parseInt(cursor.getString(
            cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
        Cursor pCur = cr.query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                                null, 
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                                new String[]{id},
                                null);

        while (pCur.moveToNext()) {
            String number = pCur.getString(pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

            contactDataMap.put(PHONE, (number != null)?number:"");
            break; // ? we want only 1 value
        } 
        pCur.close();
    }

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

    while (emailCur.moveToNext()) { 
        // This would allow you get several email addresses
        // if the email addresses were stored in an array
        String email = emailCur.getString(
                emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

        contactDataMap.put(GMAIL, (email != null)?email:"");
        break;// ? we want only 1 value
    } 
    emailCur.close();

    cursor.close();


    Log.d("activity result","onActivityResult - got contact: "+contactDataMap.get(NAME) + "; " + contactDataMap.get(GMAIL) + "; " + contactDataMap.get(PHONE));


    InputView outputViewName = getInputputViewByDisplayName(NAME, inputViewList);       
    String tempName = onGetContactInfoAppend(outputViewName, contactDataMap.get(NAME));     
    outputViewName.setValue(tempName);



    InputView outputViewGmail = getInputputViewByDisplayName(GMAIL, inputViewList);     
    String tempGmail = onGetContactInfoAppend(outputViewGmail, contactDataMap.get(GMAIL));      
    outputViewGmail.setValue(tempGmail);


    InputView outputViewPhone = getInputputViewByDisplayName(PHONE, inputViewList);
    String tempPhone = onGetContactInfoAppend(outputViewPhone, contactDataMap.get(PHONE));      
    outputViewPhone.setValue(tempPhone);
}

private static String onGetContactInfoAppend(InputView outputView, String contactData) {

    if(contactData == null){
        contactData = "";
    }

    String temp = outputView.getValue();

    //if(!"".equals(contactData)){
        if(!"".equals(temp)){
            temp = temp + " | ";
        }

        temp = temp + contactData;
    //}

    return temp;
}

private static InputView getInputputViewByDisplayName(String displayName, List<InputView> inputViewList) {
    for (InputView inputView : inputViewList){
        if (inputView.getDisplayName().equals(displayName)){
            return inputView;       
        }
    }

    return null;
}
}
于 2013-08-01T11:25:06.163 回答