如何在Android应用程序中将联系人列表中的名字显示为listview
答案 0 :(得分:1)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// We'll define a custom screen layout here (the one shown above), but
// typically, you could just use the standard ListActivity layout.
setContentView(R.layout.contacts_list_item);
Cursor mCursor = getContentResolver().query(Data.CONTENT_URI,
null, // projection
null, // selection
null, // selectionArgs
Data.DISPLAY_NAME); // sortOrder
startManagingCursor(mCursor);
// Now create a new list adapter bound to the cursor.
// SimpleListAdapter is designed for binding to a Cursor.
contactAdapter = new SimpleCursorAdapter(
this, // Context.
android.R.layout.two_line_list_item, // Specify the row template to use (here, two columns bound to the two retrieved cursor rows).
mCursor, // Pass in the cursor to bind to.
new String[] {Data.DISPLAY_NAME}, // Array of cursor columns to bind to.
new int[] {android.R.id.text1}); // Parallel array of which template objects to bind to those columns.
// Bind to our new adapter.
setListAdapter(contactAdapter);
}
如果要显示联系人姓名和号码,请将contactAdapter值更改为
contactAdapter = new SimpleCursorAdapter(
this, // Context.
android.R.layout.two_line_list_item, // Specify the row template to use (here, two columns bound to the two retrieved cursor rows).
mCursor, // Pass in the cursor to bind to.
new String[] {Data.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}, // Array of cursor columns to bind to.
new int[] {android.R.id.text1, android.R.id.text2}); // Parallel array of which template objects to bind to those columns.
快乐的编码。