簡體   English   中英

如何在Android for Android api 11+的列表視圖中顯示聯系人

[英]how to display contacts in a listview in Android for Android api 11+

我很抱歉,如果這看起來像一百萬次相同的問題...但谷歌搜索這個沒有提供結果,只是一堆過時的教程使用managedQuery和其他已棄用的解決方案......

我通過Android開發人員培訓檢索聯系人列表 ,但教程不完整,甚至下載示例代碼也無濟於事,因為示例代碼用於更高級的聯系人列表操作(搜索等)

在任何情況下,沒有理由不應該有一個簡單的解決方案,所以我希望有人可以在這里回答,因為我確信這已經做了一百萬次,我相信其他幾十個開始android開發人員我會很感激的。

沒有任何聯系人出現,我已經按照我的知識進行了教程。 我認為最重要的是TO_IDS是一個指向android.R.id.text1的整數數組。 我很困惑如何以某種方式拉出一系列聯系人姓名。

另外,我很困惑為什么當最終目標是顯示列表視圖時需要textview ...在教程中,我們有mContactsList這是一個列表視圖...但我們使用指向R.layout.contact_list_item的適配器填充列表視圖R.layout.contact_list_item ,它只是由TO_IDS填充的文本視圖,這是一個整數數組。

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

我做錯了什么,如何在列表視圖中顯示聯系人列表?

編輯:添加我的代碼:

在我的片段類中:

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

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

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

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

用於在ListView顯示聯系人姓名的小型精簡示例。 Fragment下面擴展了ListFragment ,它具有默認布局。 您無需指定自己的。 列表項的布局也取自Android的默認布局( android.R.layout.simple_list_item_1 ),這是每個項目的簡單單行文本。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM