How to get all information of an contact in android by using only 1 SQLite command?

跟風遠走 提交于 2020-01-02 09:28:31

问题


I have built an android app. It will get all contacts (1000 contact) from default address book (of my phone). And then, I show all of them on list view (of my app). BUT, I spend about 13s to load and display on list view. In my code below, I used 3 commands to query: Name, Phone Number and Company of each contact. I think this is the reason why my app which spend too much time to load and display data on listview.

I have 2 questions:

  1. How to get all information of an contact in android by using only 1 SQLite command?
  2. Is there any ways to load and display data onto listview faster???

This is code to query: name, phone no, company of a contact. The result of the query will be stored into a cursor. And then I use the cursor to set 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());
     } 
}

回答1:


I don't think so you can, or it should be something complicated.

Before fetch any info i need validate if Name exists at least.

Here is code I used:

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


来源:https://stackoverflow.com/questions/17992305/how-to-get-all-information-of-an-contact-in-android-by-using-only-1-sqlite-comma

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!