我正在创建简单的联系人应用程序,我想在列表视图中只显示名称和号码。 我使用游标适配器在简单光标适配器的列列表(FROM字符串数组)中单独加载联系人姓名和号码。但是我的输出列出了联系人电子邮件ID和重复的名称。这是我的代码。请帮我弄清楚我出错的地方单独提取姓名和号码。
import android.app.ListActivity;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class ListViewLoader extends ListActivity implements
LoaderCallbacks<Cursor> {
SimpleCursorAdapter mAdapter;
static final String[] PROJECTION = new String[] {
ContactsContract.Data._ID, ContactsContract.Data.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER };
static final String SELECTION = "((" + ContactsContract.Data.DISPLAY_NAME
+ " NOTNULL) AND (" + ContactsContract.Data.DISPLAY_NAME
+ " != '' ) AND ("
+ ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER
+ " != 0 ))";;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ProgressBar progressBar = new ProgressBar(this);
progressBar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
progressBar.setIndeterminate(true);
getListView().setEmptyView(progressBar);
ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
root.addView(progressBar);
String[] fromColumns = { ContactsContract.Data.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER };
int[] toViews = { android.R.id.text1, android.R.id.text2 };
mAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, null, fromColumns,
toViews, 0);
setListAdapter(mAdapter);
getLoaderManager().initLoader(0, null, this);
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(this, ContactsContract.Data.CONTENT_URI,
PROJECTION, SELECTION, null, null);
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
}
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
}
}
我的输出列出了我不想填充的电子邮件ID:
一个 a@gmail.com
乙 b@gmail.com
Ç c@gmail.com
d d@gmail.com
一个 (000)000-000
乙 (111)111-111
(由于我没有足够的声誉,我无法上传我的输出图像。很抱歉)