安卓。以列表视图显示联系人

发布于 2024-10-07 11:21:53 字数 1275 浏览 2 评论 0原文

我想在列表视图中显示联系人,并对所有联系人添加操作,例如单击特定联系人,它应该显示电话号码、邮件 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());

        }

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...

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

        }

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

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

发布评论

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

评论(3

暖风昔人 2024-10-14 11:21:53

当前代码中的问题是为每个循环创建新的适配器。只需移动 ArrayAdapter arr = new ArrayAdapter(this, android.R.layout.simple_list_item_1,str);
脱离do while 循环。另一个问题是,您在 UIThread 上工作太多(循环光标),因此如果您的联系人数量很大,用户将看到黑屏或 ANR。学习使用 AsyncQueryHandlerCursorAdapter,它们都在我的链接和 nikki 的链接中

,为什么不看看 Android 源代码中的默认联系人应用程序:
下面是我的自定义联系人应用程序。在此处输入图像描述

https://github.com/android/platform_packages_apps_contacts

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.enter image description here

https://github.com/android/platform_packages_apps_contacts

兲鉂ぱ嘚淚 2024-10-14 11:21:53

只需查看下面的链接并尝试使用此代码将 Android 电话簿中保存的联系人显示到您的应用程序中。

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

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

入画浅相思 2024-10-14 11:21:53

这是对您发布的代码进行一点更改,它将解决您的问题。

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文