当前位置: 首页 > 知识库问答 >
问题:

ListView示例在结果视图中重复联系人

陈项禹
2023-03-14

我试用了Android API指南中的ListView示例。我或多或少地将示例中的代码复制到了一个活动中(我现在只省略了ProgressBar)。

一切正常,只是listview显示了我的设备(运行Nexus7和Android4.2)上的每个联系人的副本。

每个联系人在ListView中重复6-7次。我使用的光标已经返回了太多的结果(它应该只返回3个项目,因为我的Nexus ATM上只有三个联系人)。当我检查raw_contact_id时,重复项总是指向相同的id值(即,我只得到3个唯一的id)。

这表明不是我的视图代码有问题。

public class ThemeSelectorActivity extends ListActivity 
                                    implements LoaderManager.LoaderCallbacks{

    private static final String TAG = "ThemeSelector";

    // The rows that we will retrieve from the db (Contacts used as dummy data)
    static final String[] PROJECTION = new String[] {ContactsContract.Data._ID,
            ContactsContract.Data.DISPLAY_NAME};

    // The select criteria for fetching contacts
    static final String SELECTION = "((" + 
            ContactsContract.Data.DISPLAY_NAME + " NOTNULL) AND (" +
            ContactsContract.Data.DISPLAY_NAME + " != '' ))";

    // The Adapter being used to display the list's data
    SimpleCursorAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        Log.d(TAG, "Create...");

        super.onCreate(savedInstanceState);

        // set the listview to be selectable
        getListView().setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);

        // For the cursor adapter, specify which columns go into which views
        String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME};
        int[] toViews = {android.R.id.text1}; // The TextView in simple_list_item_1

        // Create an empty adapter we will use to display the loaded data.
        // We pass null for the cursor, then update it in onLoadFinished()
        mAdapter = new SimpleCursorAdapter(this, 
                android.R.layout.simple_list_item_1, null,
                fromColumns, toViews, 0);

        setListAdapter( mAdapter );

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

    // Called when a new Loader needs to be created
    public Loader onCreateLoader(int id, Bundle args) {
        // Now create and return a CursorLoader that will take care of
        // creating a Cursor for the data being displayed.
        return new CursorLoader(this, ContactsContract.Data.CONTENT_URI,
                PROJECTION, SELECTION, null, null);
    }

    // Called when a previously created loader has finished loading
    public void onLoadFinished(Loader loader, Cursor data) {
        // Swap the new cursor in.  (The framework will take care of closing the
        // old cursor once we return.)
        mAdapter.swapCursor(data);
    }

    // Called when a previously created loader is reset, making the data unavailable
    public void onLoaderReset(Loader loader) {
        // This is called when the last Cursor provided to onLoadFinished()
        // above is about to be closed.  We need to make sure we are no
        // longer using it.
        mAdapter.swapCursor(null);
    }

    @Override
    /**
     * Start activity that shows a preview of the selected theme
     */
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        //String item = (String) getListAdapter().getItem(position);
        //Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
    }   
}

共有1个答案

齐雅畅
2023-03-14

contactscontract.data.content_uri显示联系人的所有数据

您应该使用contactscontact.contacts.content_uri

 类似资料:
  • 问题内容: 很抱歉,如果这看起来像是一百万次相同的问题…但是,对此进行的Google搜索没有任何结果,只是一堆使用过时的教程以及其他过时的解决方案… 我经历了如何获取联系人列表的android开发人员培训,但是本教程并不完整,即使下载示例代码也无济于事,因为示例代码用于更高级的联系人列表操作(搜索等)。 无论如何,没有理由不应该为此提供一个简单的解决方案,所以我希望有人可以在这里回答,因为我确定这

  • 我已经设法让我的应用程序在复选框状态更改时更新数据库,但当我滚动应用程序时,复选框的状态会更改。 我的问题是:如何保存复选框的状态,以便在滚动视图时它们不会更改? 这是我的光标适配器。

  • 我正在做一些性能评估的Java阿格拉捷操作,以迭代集合。我正在评估和的性能。但是我发现的输出大多数时候都是错误的。例如,在下面的代码中,有80%以上的时间输出错误: 我的问题是:我是否以错误的方式使用了?是否有任何方法可以确保的正确性。谢谢

  • 我已经将sha512密码保存到DB,我正在尝试一个简单的登录屏幕来检查密码。我总是得到一个密码错误的错误。 我重复了结果,所有文本都显示正确。我不确定这里会出什么问题? 守则的结果:

  • 本文向大家介绍thinkphp 3.2框架视图模型 实例视图查询结果的二维数组合并操作示例,包括了thinkphp 3.2框架视图模型 实例视图查询结果的二维数组合并操作示例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了thinkphp 3.2框架视图模型 实例视图查询结果的二维数组合并操作。分享给大家供大家参考,具体如下: 使用视图模型查询的时候 结果是这样的 想要的结果是这样 完整实

  • 以下网站展示了如何在JDBC中使用新的“自动关闭”功能:link。该网站显示了语句将如何自动关闭,但结果集不在try()部分,在那里它将自动关闭。所以,我的问题是,我不需要在Java 7中直接关闭结果集吗?我一直使用这种模式:关闭结果集、关闭语句、关闭连接。