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

我尝试了Android API指南中的ListView示例。 或多或少,我将示例中的代码复制到一个活动中(我现在只留下了进度条)。 除了ListView显示我在我的设备上运行的每个联系人的重复项(运行Nexus 7和Android 4.2)之外,一切正常。 每个联系人在列表视图中重复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();
    }   
}

ContactsContract.Data.CONTENT_URI显示联系人的所有数据

你应该使用ContactsContract.Contacts.CONTENT_URI

链接地址: http://www.djcxy.com/p/31989.html

上一篇: ListView example duplicates contacts in result view

下一篇: How to pass Uri value to bundle value