简体   繁体   English

Android系统。将联系人显示为列表视图

[英]Android. to display contacts as list view

I want to display contacts in list view and add actions on all contacts , like on click on a particular contact it should display the phone number , mail id and delete of the particular contact... 我想在列表视图中显示联系人并在所有联系人上添加操作,例如单击特定联系人时应显示电话号码,邮件ID和删除特定联系人...

import android.app.ListActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CPDemo1 extends ListActivity {


    @SuppressWarnings("unchecked")
 public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
     String str[]=    {"datta","vivek","Nagesh sir","shiv"};
     String name; 

        ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        int nameIdx = cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME);

        if (cursor.moveToFirst())


         do {

         int x = 0;

         name = cursor.getString(nameIdx);
         str[x]= name;
                 x++;
          ArrayAdapter arr = new ArrayAdapter(this, android.R.layout.simple_list_item_1,str);

          setListAdapter(arr);
 } while(cursor.moveToNext());

        }

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); 只需移动ArrayAdapter arr = new ArrayAdapter(this, android.R.layout.simple_list_item_1,str); out of do while loop . out 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. 还有一个问题,你在UIThread (循环光标)上工作太多,所以如果你的联系人数很多,用户会看到黑屏或ANR。 Learn to use AsyncQueryHandler and CursorAdapter , it's all in my link and nikki's link 学习使用AsyncQueryHandlerCursorAdapter ,它都在我的链接和nikki的链接中

And why don't you have a look at default Contacts app in Android source code: Below is my custom Contacts App. 为什么不看看Android源代码中的默认联系人应用程序:下面是我的自定义联系人应用程序。 在此输入图像描述

https://github.com/android/platform_packages_apps_contacts https://github.com/android/platform_packages_apps_contacts

Just have a look at below link and try using this code for displaying contacts saved in android phone book into your application. 只需看看下面的链接,并尝试使用此代码将保存在Android电话簿中的联系人显示到您的应用程序中。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM