Android. to display contacts as list view

吃可爱长大的小学妹 提交于 2019-12-03 06:30:45

The problem in your current code is create new adapter for every loop. Just move ArrayAdapter arr = new ArrayAdapter(this, android.R.layout.simple_list_item_1,str); out of do while loop. And another issue, You work too much on UIThread (loop through cursor )so user will see a black screen or ANR if your numbers of contact is huge. Learn to use AsyncQueryHandler and CursorAdapter, it's all in my link and nikki's link

And why don't you have a look at default Contacts app in Android source code: Below is my custom Contacts App.

https://github.com/android/platform_packages_apps_contacts

Nikki

Just have a look at below link and try using this code for displaying contacts saved in android phone book into your application.

http://developer.android.com/resources/samples/ContactManager/index.html

VISHAL DAGA

Here is a little change to the code posted by you, it will solve your problem.

if (cursor.moveToFirst())
    {

        int x = 0;
     do {



     name = cursor.getString(nameIdx);
     Log.d("CONTENT",name);
     str[x]= name;
             x++;
      } while(cursor.moveToNext());

     ArrayAdapter<String> arr = new ArrayAdapter<String>(
        this, android.R.layout.simple_list_item_1,str);

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