首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Android for Android api 11+的列表视图中显示联系人

如何在Android for Android api 11+的列表视图中显示联系人
EN

Stack Overflow用户
提问于 2013-08-13 09:41:01
回答 1查看 23.7K关注 0票数 17

我很抱歉,如果这看起来像是同样的问题一百万个times...but谷歌搜索没有提供任何结果,只有一堆过时的教程使用managedQuery和其他过时的解决方案…

我浏览了android developer training for retrieving a contact list,但教程并不完整,即使下载示例代码也无济于事,因为示例代码用于更高级的联系人列表操作(搜索等)。

在任何情况下,没有理由不应该有一个简单的解决方案,所以我希望有人能在这里回答,因为我相信这已经被做了一百万次,我相信其他几十个android初学者会喜欢这个。

据我所知,我一直遵循教程,没有任何联系人出现。我认为最重要的是TO_IDS是一个指向android.R.id.text1的整数数组。我搞不懂这是怎么拉出一组联系人姓名的。

另外,我也搞不懂为什么需要一个文本视图,而最终目标是显示一个listview...In。我们有一个mContactsList,它是一个列表view...But,我们用一个指向R.layout.contact_list_item的适配器填充列表视图,它就是由TO_IDS填充的文本视图,一个整数数组。

代码语言:javascript
复制
mContactsList = (ListView) getActivity().findViewById(R.layout.contact_list_view);
mCursorAdapter = new SimpleCursorAdapter(
            getActivity(),
            R.layout.contact_list_item,
            null,
            FROM_COLUMNS, TO_IDS,
            0);
mContactList.setAdapter(mCursorAdapter);

我做错了什么?如何在列表视图中简单地显示联系人列表?

编辑:添加到我的代码中:

在我的片段类中:

代码语言:javascript
复制
public class MyFragment extends Fragment implements
    LoaderManager.LoaderCallbacks<Cursor>{

private static final String[] FROM_COLUMNS = {ContactsContract.Contacts.DISPLAY_NAME_PRIMARY };
private static final int[] TO_IDS = {android.R.id.text1};
ListView mContactList;
private SimpleCursorAdapter mCursorAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    return inflater.inflate(R.layout.contact_list_view,container,false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    mContactsList = (ListView) getActivity().findViewById(R.layout.contact_list_view);
    mCursorAdapter = new SimpleCursorAdapter(
            getActivity(),
            R.layout.contact_list_item,
            null,
            FROM_COLUMNS, TO_IDS,
            0);
    mContactList.setAdapter(mCursorAdapter);

}

@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
    return null;
}

@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {

}

@Override
public void onLoaderReset(Loader<Cursor> cursorLoader) {

}
}

在我的activity_main.xml中:

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment
            android:id ="@+id/contactListFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:name="preamble.myapp.MyFragment"/>
</LinearLayout>

在我的contact_list_view xml中:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<ListView   xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@android:id/list"
            android:layout_height="match_parent"
            android:layout_width="match_parent"/>

在我的contact_list_item xml中

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@android:id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

最后,对于contact_list_layout xml:

我应该在contact_list_layout.xml中放入什么?这只是一个空的<LinearLayout>吗?在本教程中并不清楚如何处理此xml。它说这个XML就是片段,但是如果它是片段,为什么我们还要在contact_list_view.xml中定义一个listview

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18199359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档