Dat*_*tta 9 android listview listactivity android-contacts
我想在列表视图中显示联系人并在所有联系人上添加操作,例如点击特定联系人时应显示电话号码,邮件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());
}
Run Code Online (Sandbox Code Playgroud)
Nam*_*ung 10
当前代码中的问题是为每个循环创建新的适配器.刚搬出ArrayAdapter arr = new ArrayAdapter(this, android.R.layout.simple_list_item_1,str);
去do while loop.另一个问题,你工作太多UIThread(循环光标),所以如果你的联系人数量很大,用户将会看到黑屏或ANR.学会使用AsyncQueryHandler和CursorAdapter,这一切都在我的链接和nikki的链接中
为什么不看看Android源代码中的默认联系人应用程序:下面是我的自定义联系人应用程序.
https://github.com/android/platform_packages_apps_contacts
只需查看下面的链接并尝试使用此代码将 Android 电话簿中保存的联系人显示到您的应用程序中。
http://developer.android.com/resources/samples/ContactManager/index.html
| 归档时间: |
|
| 查看次数: |
26976 次 |
| 最近记录: |