繁体   English   中英

如何在Android应用程序中从联系人列表到列表视图显示名字

[英]how to display first name from contact list to listview in android application

如何在Android应用程序中从联系人列表到列表视图显示名字

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.

快乐的编码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM