Android:快速查找联系人列表

发布于 2024-10-10 23:06:07 字数 249 浏览 7 评论 0原文

我目前正在开发一个应用程序,需要通过电话号码查找一些联系人(姓名和照片)。 但是,这需要几秒钟(对于每个联系人:通过电话号码查找联系人,获取联系人的姓名和照片)。

加速这一过程的好策略是什么?我意识到我可以使用自己的 sqlite 数据库,其中包含我需要的姓名和照片的列表。使用这样的数据库,我只能执行一次查询,然后立即获取我需要的所有联系人的数据。然而,这会增加相当多的开销,我希望避免。

有更好的(说:更简单的)解决方案吗?

谢谢。

I am currently developing an App which needs to look up a number of contacts (name and photo) via their phone number.
However, this takes several seconds (For each contact: Look up the contact via their phone number, get contact's name & photo).

What would be a good strategy to speed this process up? I realize that I could use my own sqlite db which contains a list of exactly those names and photos I need. With such a database I could only do one query, then get the data for all contacts I need at once. This however would add quite some overhead I am hoping to avoid.

Is there a better (speak: simpler) solution?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月依秋水 2024-10-17 23:06:07

您可以使用 CursorAdapter 并覆盖 bindView() 方法,例如:

@Override
public void bindView(final View view, Context context, final Cursor cursor) {
    holder = (ItemHolder) view.getTag();
    ImageView icon = holder.getImageView();
    TextView name = holder.getName();
    final TextView email = holder.getEmail();
    icon.setBackgroundResource(R.drawable.contact_icon);
    name.setText(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));


    final int contactid = cursor.getInt(cursor.getColumnIndex("_id"));
    mHandler.post(new Runnable() {

        @Override
        public void run() {
            Cursor emailCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, "contact_id=?", new String[]{String.valueOf(contactid)}, null);
            startManagingCursor(emailCursor);
//          Log.i("cursor:", emailCursor.getCount()+"");
            if(emailCursor!=null){

                if(emailCursor.getCount()==0){
                    email.setText("");
                }else {
                    while(emailCursor.moveToNext()){
                        String emails = emailCursor.getString(emailCursor.getColumnIndex("data1"));
                        email.setText(emails);
                    }
                }

            }
            emailCursor.close();
        }
    });

您可以使用 ContactsContract.CommonDataKinds.Phone.CONTENT_URI 而不是 ContactsContract.CommonDataKinds.Email.CONTENT_URI 。

you can use CursorAdapter and override bindView() method for example:

@Override
public void bindView(final View view, Context context, final Cursor cursor) {
    holder = (ItemHolder) view.getTag();
    ImageView icon = holder.getImageView();
    TextView name = holder.getName();
    final TextView email = holder.getEmail();
    icon.setBackgroundResource(R.drawable.contact_icon);
    name.setText(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));


    final int contactid = cursor.getInt(cursor.getColumnIndex("_id"));
    mHandler.post(new Runnable() {

        @Override
        public void run() {
            Cursor emailCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, "contact_id=?", new String[]{String.valueOf(contactid)}, null);
            startManagingCursor(emailCursor);
//          Log.i("cursor:", emailCursor.getCount()+"");
            if(emailCursor!=null){

                if(emailCursor.getCount()==0){
                    email.setText("");
                }else {
                    while(emailCursor.moveToNext()){
                        String emails = emailCursor.getString(emailCursor.getColumnIndex("data1"));
                        email.setText(emails);
                    }
                }

            }
            emailCursor.close();
        }
    });

you would use ContactsContract.CommonDataKinds.Phone.CONTENT_URI instead of ContactsContract.CommonDataKinds.Email.CONTENT_URI .

转身泪倾城 2024-10-17 23:06:07

您可以使用 Hashmap 来缓存数据,运行后台线程将联系人数据缓存在 HashMap 中,并在需要时从 hashmap 中获取数据。当需要信息时,它将减少查询时间。

You can use Hashmap to cache the data, run a background thread to cache the contacts data in HashMap and fetch it from hashmap when ever necessary. It will reduce query time when information is needed.

世界如花海般美丽 2024-10-17 23:06:07

另一个例子:

       ListView myListView = (ListView)findViewById(R.id.myListView);

   Cursor cur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

   startManagingCursor(cur);


   ListAdapter adapter = new SimpleCursorAdapter(this,
   android.R.layout.simple_list_item_2,
   cur,
   new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER},new int[] {android.R.id.text1, android.R.id.text2});
   myListView.setAdapter(adapter);

another example:

       ListView myListView = (ListView)findViewById(R.id.myListView);

   Cursor cur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

   startManagingCursor(cur);


   ListAdapter adapter = new SimpleCursorAdapter(this,
   android.R.layout.simple_list_item_2,
   cur,
   new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER},new int[] {android.R.id.text1, android.R.id.text2});
   myListView.setAdapter(adapter);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文