Android系统。显示联系人的列表视图 [英] Android. to display contacts as list view
问题描述
我要显示在列表中查看联系人和所有联系人上点击一个特定的联系人添加动作,像它应该显示电话号码,邮件ID和特定联系人的删除...
进口android.app.ListActivity;
进口android.content.ContentResolver;
进口android.database.Cursor;
进口android.os.Bundle;
进口android.provider.ContactsContract;
进口android.view.Menu;
进口android.view.View;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;
进口android.widget.TextView;
进口android.widget.Toast;公共类CPDemo1扩展ListActivity {
@燮pressWarnings(未登记)
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
字符串str [] = {达塔,维韦克,纳格什先生,毒刃};
字符串名称; ContentResolver的CR = getContentResolver();
光标光标= cr.query(ContactsContract.Contacts.CONTENT_URI,NULL,NULL,NULL,NULL);
INT nameIdx = cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME); 如果(cursor.moveToFirst())
做{ INT X = 0; 名称= cursor.getString(nameIdx);
海峡[X] =名称;
X ++;
ArrayAdapter ARR =新ArrayAdapter(这一点,android.R.layout.simple_list_item_1,STR); setListAdapter(ARR);
}而(cursor.moveToNext()); }
在当前的code这个问题是每一个循环创造新的适配器。只是移动 ArrayAdapter ARR =新ArrayAdapter(这一点,android.R.layout.simple_list_item_1,STR);
出 do while循环
。而另外一个问题,您对工作 UIThread
过多(环通光标),因此用户会看到一个黑色的屏幕或ANR如果您的联系人的数量是巨大的。了解如何使用 AsyncQueryHandler
和的CursorAdapter
,这一切都在我的链接和Nikki的链接
和你为什么不看看在Android源$ C $ C默认通讯录应用程序:
下面是我的自定义联系人应用程序。
<一个href=\"https://github.com/android/platform_packages_apps_contacts\">https://github.com/android/platform_packages_apps_contacts
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());
}
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
这篇关于Android系统。显示联系人的列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!