gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:11:19 24 4
gpt4 key购买 nike

我正在关注 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com