如何在Android api 11+的Android列表视图中显示联系人 [英] how to display contacts in a listview in Android for Android api 11+

查看:33
本文介绍了如何在Android api 11+的Android列表视图中显示联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm sorry if this looks like the same question a million times...but a google search for this provides no results, just a bunch of outdated tutorials using managedQuery and other deprecated solutions...

我参加了 android 开发人员检索联系人列表的培训,但教程不完整,甚至下载示例代码也无济于事,因为示例代码用于更高级的联系人列表操作(搜索等)

I went through the android developer training for retrieving a contact list, but the tutorial is incomplete and even downloading the sample code doesn't help because the sample code is for more advanced contact list manipulation (search, etc.)

无论如何,没有理由不应该有一个简单的解决方案,所以我希望有人能在这里回答,因为我确定这已经完成了一百万次,而且我确定还有其他几十次刚开始的 Android 开发者会很欣赏这一点.

In any case, there is no reason why there should not be a simple solution to this so I'm hoping someone can answer here because I'm sure this has been done a million times and I'm sure dozens of other beginning android developers would appreciate this.

据我所知,我已按照教程进行操作,没有出现任何联系人.我认为最重要的是 TO_IDS 是一个指向 android.R.id.text1 的整数数组.我很困惑这应该如何以某种方式拉出一组联系人姓名.

I have followed the tutorial to the best of my knowledge by no contacts show up. I think the biggest thing is that the TO_IDS is an integer array that points to android.R.id.text1. I'm confused how that is supposed to somehow pull an array of contact names.

另外,当最终目标是显示列表视图时,我很困惑为什么需要文本视图......在本教程中,我们有 mContactsList,它是一个列表视图......但我们用适配器填充列表视图指向 R.layout.contact_list_item,它只是由整数数组 TO_IDS 填充的文本视图.

Also, I'm confused why a textview is needed when the end goal is to display a listview...In the tutorial, we have mContactsList which is a list view...But we populate the list view with an adapter pointing to R.layout.contact_list_item which is just textviews populated by TO_IDS, an array of integers.

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

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

What am I doing wrong and how do I simply display the contact list in a listview?

添加我的代码:

在我的片段类中:

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中:

in my activity_main.xml:

<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 中:

in my contact_list_view xml :

<?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 中

In my contact_list_item xml

<?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:

finally for contact_list_layout xml:

我在 contact_list_layout.xml 中放了什么?这只是一个空的 吗?教程中并不清楚这个xml是如何处理的.它说这个 XML 是片段,但如果它是片段,为什么我们要在 contact_list_view.xml 中定义一个 listview ?

What do I put in the contact_list_layout.xml? Is this just an empty <LinearLayout>? It's not clear in the tutorial how this xml is handled. It says this XML is the fragment, but if it's the fragment, why did we define a listview already in the contact_list_view.xml?

推荐答案

用于在 ListView 中显示联系人姓名的精简示例.Fragment 下面扩展了具有默认布局的 ListFragment.您不需要指定自己的.列表项的布局也取自 Android 的默认布局 (android.R.layout.simple_list_item_1),它是每个项目的简单单行文本.

Small stripped down example for displaying the contacts name in a ListView. Below Fragment extends ListFragment which has a default layout. You don't need to specify your own. The layout for list items is also taken from Android's default layouts (android.R.layout.simple_list_item_1) which is a simple single line of text per item.

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;

public class ContactListFragment extends ListFragment implements LoaderCallbacks<Cursor> {

    private CursorAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // create adapter once
        Context context = getActivity();
        int layout = android.R.layout.simple_list_item_1;
        Cursor c = null; // there is no cursor yet
        int flags = 0; // no auto-requery! Loader requeries.
        mAdapter = new SimpleCursorAdapter(context, layout, c, FROM, TO, flags);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // each time we are started use our listadapter
        setListAdapter(mAdapter);
        // and tell loader manager to start loading
        getLoaderManager().initLoader(0, null, this);
    }

    // columns requested from the database
    private static final String[] PROJECTION = {
        Contacts._ID, // _ID is always required
        Contacts.DISPLAY_NAME_PRIMARY // that's what we want to display
    };

    // and name should be displayed in the text1 textview in item layout
    private static final String[] FROM = { Contacts.DISPLAY_NAME_PRIMARY };
    private static final int[] TO = { android.R.id.text1 };

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {

        // load from the "Contacts table"
        Uri contentUri = Contacts.CONTENT_URI;

        // no sub-selection, no sort order, simply every row
        // projection says we want just the _id and the name column
        return new CursorLoader(getActivity(),
                contentUri,
                PROJECTION,
                null,
                null,
                null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        // Once cursor is loaded, give it to adapter
        mAdapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        // on reset take any old cursor away
        mAdapter.swapCursor(null);
    }
}

这篇关于如何在Android api 11+的Android列表视图中显示联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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