如何从整个联系人中检索唯一的电子邮件 ID?
Posted
技术标签:
【中文标题】如何从整个联系人中检索唯一的电子邮件 ID?【英文标题】:How can I retrieve the only email id from the entire Contacts? 【发布时间】:2012-06-14 08:46:00 【问题描述】:我的main.xml
文件中有一个按钮。当我单击按钮时,它将显示电子邮件列表
整个联系人列表的 id。这意味着检索联系人的电子邮件 ID(如果有)。我需要
电子邮件 ID 不是联系人。有什么解决办法吗?
【问题讨论】:
【参考方案1】:请查看代码。
Public ArrayList<String> getNameEmailDetails()
ArrayList<String> names = new ArrayList<String>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0)
while (cur.moveToNext())
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]id, null);
while (cur1.moveToNext())
//to get the contact names
String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.e("Name :", name);
String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.e("Email", email);
if(email!=null)
names.add(name);
cur1.close();
return names;
【讨论】:
您的代码有一些错误我在按钮单击时尝试了整个代码【参考方案2】:试试下面的,
package stack.examples;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class ***GetContactsActivity extends Activity
ListView lvItem;
private Button btnAdd;
String displayName="", emailAddress="", phoneNumber="";
ArrayList<String> contactlist=new ArrayList<String>();
ArrayAdapter<String> itemAdapter;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lvItem = (ListView)this.findViewById(R.id.listView_items);
btnAdd = (Button)this.findViewById(R.id.btnAddItem);
itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,contactlist);
lvItem.setAdapter(itemAdapter);
btnAdd.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
GetEmails();
);
private void GetEmails()
ContentResolver cr =getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext())
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor emails = cr.query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);
while (emails.moveToNext())
emailAddress = emails.getString(emails.getColumnIndex(Email.DATA));
contactlist.add(emailAddress);
itemAdapter.notifyDataSetChanged();
emails.close();
cursor.close();
【讨论】:
以上是关于如何从整个联系人中检索唯一的电子邮件 ID?的主要内容,如果未能解决你的问题,请参考以下文章