android - 为什么联系人在 ListView 中重复?

标签 android

我正在关注 android 网站上的一个例子。我是 android 开发的新手。我遇到的问题是我的联系人一遍又一遍地重复,大约 6 次。谁能弄清楚为什么?我觉得这可能与我的进口有关,因为它们没有包含在示例中,但我不确定。另请注意,我没有为 res 下的 listview 创建 xml 文件。 谢谢,

ListViewLoader.java

package com.example.contactlist;

import android.app.ListActivity;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;

//from http://developer.android.com/guide/topics/ui/layout/listview.html

public class ListViewLoader extends ListActivity implements
        LoaderManager.LoaderCallbacks<Cursor> {

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

    // These are the Contacts rows that we will retrieve
    static final String[] PROJECTION = new String[] {
            ContactsContract.Data._ID, ContactsContract.Data.DISPLAY_NAME };

    // This is the select criteria
    static final String SELECTION = "((" + ContactsContract.Data.DISPLAY_NAME
            + " NOTNULL) AND (" + ContactsContract.Data.DISPLAY_NAME
            + " != '' ))";

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


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

    // Called when a previously created loader is reset, making the data
    // unavailable
    public void onLoaderReset(Loader<Cursor> 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
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Do something when a list item is clicked
    }
}

最佳答案

我找到了解决方案。我需要查询联系人表而不是数据表。显然数据表有重复项。我粘贴了一部分更新后的代码。

public class ListViewLoader extends ListActivity implements
        LoaderManager.LoaderCallbacks<Cursor> {



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

    // These are the Contacts rows that we will retrieve
    static final String[] PROJECTION = new String[] {
            ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME };

    // This is the select criteria
    static final String SELECTION = "((" + ContactsContract.Contacts.DISPLAY_NAME
            + " NOTNULL) AND (" + ContactsContract.Contacts.DISPLAY_NAME
            + " != '' ))";

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


        // For the cursor adapter, specify which columns go into which views
        String[] fromColumns = { ContactsContract.Contacts.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);
        System.out.println("oncreate (Bundle)");        
    }

    // Called when a new Loader needs to be created
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        // Now create and return a CursorLoader that will take care of
        // creating a Cursor for the data being displayed.

        System.out.println("oncreateloader");
        System.out.println(PROJECTION);
        System.out.println(SELECTION);

        return new CursorLoader(this, ContactsContract.Contacts.CONTENT_URI,
                PROJECTION, SELECTION, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");

    }

关于android - 为什么联系人在 ListView 中重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21494758/

相关文章:

android - 我在特定时间安排本地通知,使用开关但它不会在设定时间触发

android - Android Studio-Gradle-根据每个Android Studio安装而不是每个项目设置代理

android - 是否可以在单个项目中使用 SEND-SMS 和 RECEIVE_SMS 使用权限?

android如何访问功能模块xml文件中的基本模块可绘制对象

android - ListView 中带有圆角的图像

java - Android WebView - JavaScript 内存泄漏

android - 在 onDestroy() 将变量从服务传输到已经打开的 Main-Activity

android - 我如何在 Android 中写入/cache/recovery/command?

android - onStart() 和 onResume() 的区别

android - 将文件发送到 WiFi 打印机