Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 缩短获得电话号码的时间_Android_Contacts - Fatal编程技术网

Android 缩短获得电话号码的时间

Android 缩短获得电话号码的时间,android,contacts,Android,Contacts,我必须创建一个列表视图,其中包含手机上所有联系人的姓名和电话号码。 我的代码正在运行,但是当用户有很多联系人时,它占用了太多的时间。 你知道我该如何改进它吗 public List<Contact> fetchContacts () { String phoneNumber = null; String email = null; Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;

我必须创建一个列表视图,其中包含手机上所有联系人的姓名和电话号码。 我的代码正在运行,但是当用户有很多联系人时,它占用了太多的时间。 你知道我该如何改进它吗

    public List<Contact> fetchContacts ()  {

    String phoneNumber = null;
    String email = null;

    Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
    String _ID = ContactsContract.Contacts._ID;
    String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
    String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;

    Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
    String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
    ContentResolver contentResolver = getContentResolver();

    Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null, DISPLAY_NAME + " ASC");

    List<Contact> contactList = new ArrayList<Contact>();

   // Loop for every contact in the phone
    if (cursor.getCount() > 0) {
        while (cursor.moveToNext()) {

            String contact_id = cursor.getString(cursor.getColumnIndex( _ID ));
            String name = cursor.getString(cursor.getColumnIndex( DISPLAY_NAME ));
            int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex( HAS_PHONE_NUMBER )));
            if (hasPhoneNumber > 0) {
                Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[]{contact_id}, null);
                phoneCursor.moveToNext();
                phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
                phoneCursor.close();
                Contact contacto = new Contact();
                contacto.name = name;
                contacto.phone = phoneNumber;
                contacto.id = contact_id;
                contactList.add(contacto);
            }
        }
    }

    return contactList;
}
公共列表获取联系人(){
字符串phoneNumber=null;
字符串email=null;
Uri CONTENT\u Uri=Contacts contract.Contacts.CONTENT\u Uri;
String _ID=Contacts contact.Contacts.\u ID;
String DISPLAY\u NAME=Contacts contract.Contacts.DISPLAY\u NAME;
字符串HAS\u PHONE\u NUMBER=Contacts contract.Contacts.HAS\u PHONE\u NUMBER;
Uri PhoneCONTENT\u Uri=ContactsContract.CommonDataTypes.Phone.CONTENT\u Uri;
字符串Phone\u CONTACT\u ID=contacts contract.commondatatypes.Phone.CONTACT\u ID;
字符串编号=contacts contract.commonDataTypes.Phone.NUMBER;
ContentResolver ContentResolver=getContentResolver();
Cursor Cursor=getContentResolver().query(CONTENT_URI,null,null,null,DISPLAY_NAME+“ASC”);
List contactList=new ArrayList();
//为手机中的每个联系人循环
if(cursor.getCount()>0){
while(cursor.moveToNext()){
String contact_id=cursor.getString(cursor.getColumnIndex(_id));
字符串名称=cursor.getString(cursor.getColumnIndex(DISPLAY_name));
int hasPhoneNumber=Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER));
如果(hasPhoneNumber>0){
游标phoneCursor=contentResolver.query(PhoneCONTENT\u URI,null,Phone\u CONTACT\u ID+“=?”,新字符串[]{CONTACT\u ID},null);
phoneCursor.moveToNext();
phoneNumber=phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
phoneCursor.close();
触点O=新触点();
contacto.name=姓名;
contacto.phone=电话号码;
contacto.id=联系人的id;
联系人列表。添加(contacto);
}
}
}
返回联系人列表;
}

您正在一次提取手机上的所有联系人。这就是它需要时间的原因。您应该改为使用游标适配器:

您可以定义从(选择)、到(投影)和列表项视图布局:

public SimpleCursorAdapter(android.content.Context context, int layout, android.database.Cursor c, java.lang.String[] from, int[] to, int flags) { /* compiled code */ }
然后在ListView上使用以下设置适配器:

mListView.setAdapter(mAdapter);
适配器所做的实际上是分块获取联系人,仅用于可见视图(带有一些额外的),而不是一次获取所有联系人

另一个提示:您需要实现:

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
...
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
   ...
}
bindView应该只绑定来自光标的数据,newView创建新视图并设置指向内部视图的指针,以便在bindView中更快地访问(基本上,您应该在newView中执行所有.findViewById(…)


提示2:在listView中寻找ViewHolder模式以加快视图循环

不要将您的Google搜索放在问题的标题中。将您期望从Google搜索中看到的实际搜索结果放在问题的标题中。