为什么在ListView中联系人会重复显示?

6
我正在按照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
    }
}

希望这个网站能够帮到你! - Rajendra arora
这个例子似乎和我的不同,因为他们使用了不同的方法,所以很难将他们的解决方案应用到我的问题上。当我尝试在Stack Overflow上搜索其他示例时,也遇到了同样的问题。 - Kt Mack
2个回答

4

我已经找到了解决方案。我需要查询Contacts表而不是Data表。显然,Data表中有重复项。下面是我的更新代码的一部分。

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

    }

谢谢,它像魔法一样工作,但是我怎么能通过名字获取联系电话呢? - Fatal Exception

-1

尝试更改

mAdapter.swapCursor(data) 

mAdapter.changeCursor(cursor) 

mAdapter.swapCursor(null) 

mAdapter.changeCursor(null)

感谢您提供的解决方案,尽管我尝试了这个方法,但联系人仍然重复出现。通过进一步的阅读,我发现可能是由于联系人在多个组中导致了重复。我无法确定如何检查这个问题,但这有可能是原因。听说如果这是问题的话,将它们导入到单独的数据库中可能是一个解决办法。目前还没有实施解决方案的能力。 - Kt Mack

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接