我想在列表视图中显示联系人,并对所有联系人添加操作,例如单击特定联系人,它应该显示电话号码、邮件 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());
}
当前代码中的问题是为每个循环创建新的适配器。只要移动ArrayAdapter arr = new ArrayAdapter(this, android.R.layout.simple_list_item_1,str);
out of do while loop
。还有一个问题,你工作太多了UIThread
(循环光标)因此,如果您的联系人数量很大,用户将看到黑屏或 ANR。学会使用AsyncQueryHandler
and CursorAdapter
,全部在我的链接和 nikki 的链接中
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 https://github.com/android/platform_packages_apps_contacts
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)