开发者

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

开发者 https://www.devze.com 2023-02-14 04:29 出处:网络
how to display first name from contact list to lis开发者_高级运维tview in android applicationpublic void onCreate(Bundle savedInstanceState) {

how to display first name from contact list to lis开发者_高级运维tview in android application


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);

}

if you want to display contact name and number both then change contactAdapter value as

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.

happy coding.

0

精彩评论

暂无评论...
验证码 换一张
取 消