【问题标题】:How TO: obtain the list of names from the phonebook to be used in a list view?如何:从电话簿中获取要在列表视图中使用的姓名列表?
【发布时间】:2010-07-16 00:10:51
【问题描述】:

这可能是一个非常简单的答案,但我似乎无法找到答案,我的目标是 android 2.1。

任何帮助都会很棒:-)

【问题讨论】:

    标签: android android-widget


    【解决方案1】:

    在您的 Activity 或其他应用程序组件中试试这个:

    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(People.CONTENT_URI, null, null, null, null);
    if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                    String name = formatName(cur.getString(cur
                                    .getColumnIndex(People.DISPLAY_NAME)));
                    // Do something with name.
            }
    }
    

    【讨论】:

      【解决方案2】:
       First you have to add `ListView` in your layout file   
      
      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">
          <ListView
              android:id="@+id/list"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"/>
      </LinearLayout>
      

      现在在您的Activity 中使用此代码:

          Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
          startManagingCursor(c);
      
          ListAdapter adapter = new SimpleCursorAdapter(this, 
                  android.R.layout.simple_list_item_1, 
                  c, 
                  new String[] {People.NAME} ,
                  new int[] {android.R.id.text1}); 
          setListAdapter(adapter);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-04
        • 1970-01-01
        • 2012-01-02
        • 2011-12-20
        • 2023-04-02
        • 2014-12-27
        相关资源
        最近更新 更多