【问题标题】:Android data in ListViewListView 中的 Android 数据
【发布时间】:2014-09-24 21:51:01
【问题描述】:

我正在开发这个联系人应用程序。到目前为止,我已经生成了一个具有联系人姓名和电话号码的 ListView 女巫。当您单击联系人时,它会启动新活动并显示联系人姓名和电话号码。

我想做的是我的 ListView 只显示联系人姓名,当您单击列表中的联系人时,活动开始,您可以看到姓名和号码。

我想也许我可以在 ListView 中隐藏一些信息,但我没有找到任何好的东西。

那么有人有什么建议吗?

提前致谢。

【问题讨论】:

  • 不确定您在问什么 - “当您单击联系人时,它会启动新活动并显示联系人姓名和电话号码。”非常类似于“当您单击列表中的联系人时,活动开始,您可以看到姓名和号码。”
  • okei 对不起,我有点不清楚,程序启动时首先提到的是它显示联系人列表,我希望它只显示联系人的姓名。但现在它同时显示姓名和电话号码。

标签: android listview android-listview


【解决方案1】:

首先,只查询联系人姓名和id:

您必须在清单中声明

<uses-permission android:name="android.permission.READ_CONTACTS" />
public Loader<Cursor> onCreateLoader(int loaderID, Bundle bundle){
       Uri uri                = ContactsContract.Contacts.CONTENT_URI;
       String[] projection    = new String[] { ContactsContract.Contacts._ID,
                                    ContactsContract.Contacts.DISPLAY_NAME};
       String sortOrder       = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

        // Returns a new CursorLoader
        return new CursorLoader(
                    getActivity(),   // Parent activity context
                    uri,        // Table to query
                    projection,     // Projection to return
                    null,            // No selection clause
                    null,            // No selection arguments
                    sortOrder        // Sort by name
    );

}

一旦您获得带有联系人的光标,您必须将其传递给 CursorAdapter

private final static String[] FROM_COLUMNS = {
        Build.VERSION.SDK_INT
                >= Build.VERSION_CODES.HONEYCOMB ?
                Contacts.DISPLAY_NAME_PRIMARY :
                Contacts.DISPLAY_NAME
};
private final static int[] TO_IDS = {
       android.R.id.text1
};


public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ...
    // Gets the ListView from the View list of the parent activity
    mContactsList =
        (ListView) getActivity().findViewById(R.layout.contact_list_view);
    // Gets a CursorAdapter
    mCursorAdapter = new SimpleCursorAdapter(
            getActivity(),
            R.layout.contact_list_item,
            null,
            FROM_COLUMNS, TO_IDS,
            0);
    // Sets the adapter for the ListView
    mContactsList.setAdapter(mCursorAdapter);

    // Prepare the loader.  Either re-connect with an existing one,
    // or start a new one.
    getLoaderManager().initLoader(0, null, this);

}

public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    // Swap the new cursor in.  (The framework will take care of closing the
    // old cursor once we return.)
    mAdapter.swapCursor(data);

    // The list should now be shown.
    if (isResumed()) {
        setListShown(true);
    } else {
        setListShownNoAnimation(true);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多