如何从联系人列表中显示的名字在Android的应用程序的ListView [英] how to display first name from contact list to listview in android application

查看:79
本文介绍了如何从联系人列表中显示的名字在Android的应用程序的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从联系人列表中的Andr​​oid应用程序中显示的名字,以列表视图

解决方案

 公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);


    //我们将定义一个定制的屏幕布局位置(如上所示),但
    //通常情况下,你可以只使用标准的ListActivity布局。
    的setContentView(R.layout.contacts_list_item);

    光标mCursor = getContentResolver()查询(Data.CONTENT_URI,
                    空,//投影
                    空,//选择
                    空,// selectionArgs两个
                    Data.DISPLAY_NAME); // 排序

    startManagingCursor(mCursor);



    //现在创建一个新的列表适配器绑定到光标。
    // SimpleListAdapter被设计用于结合到一个光标。
    contactAdapter =新SimpleCursorAdapter(
            对此,语境//。
            android.R.layout.two_line_list_item,//指定排模板使用(在这里,两列开往两个检索光标行)。
            mCursor,//通行证光标结合。
            新的String [] {} Data.DISPLAY_NAME,//光标柱阵列结合。
            新的INT [] {android.R.id.text1}); //该模板对象的并行阵列绑定到这些列。

    //绑定到我们新的适配器。
    setListAdapter(contactAdapter);

}
 

如果你想显示联系人的姓名和电话号码既然后更改contactAdapter值

  contactAdapter =新SimpleCursorAdapter(
            对此,语境//。
            android.R.layout.two_line_list_item,//指定排模板使用(在这里,两列开往两个检索光标行)。
            mCursor,//通行证光标结合。
            新的String [] {Data.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER} //光标柱阵列结合。
            新的INT [] {android.R.id.text1,android.R.id.text2}); //该模板对象的并行阵列绑定到这些列。
 

快乐编码。

how to display first name from contact list to listview 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.

这篇关于如何从联系人列表中显示的名字在Android的应用程序的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆