我正在使用的代码会在列表视图中显示名称和号码,但我也需要显示电子邮件,因为我添加了一些代码段,但电子邮件未显示在列表视图中。我使用的是Android 2.1平台。
CODE
public class Trial2 extends Activity {
/** Called when the activity is first created. */
private static final int CONTACT_PICKER_RESULT = 1001;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button Btn = (Button)findViewById(R.id.button1);
Btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(i, CONTACT_PICKER_RESULT);
}
});
}
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if(resultCode == RESULT_OK) {
switch (reqCode) {
case CONTACT_PICKER_RESULT:
Cursor cursor = null;
Cursor Emails = null;
String number = "";
String mailid;
try {
Uri result = data.getData();
//get the id from the uri
String id = result.getLastPathSegment();
//query
cursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone._ID + " = ? " , new String[] {id}, null);
int numberIdx = cursor.getColumnIndex(Phone.DATA);
if(cursor.moveToFirst()) {
number = cursor.getString(numberIdx);
} else {
}
Emails = getContentResolver().query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);
int emailid = Emails.getColumnIndex(Email.DATA);
if(Emails.moveToFirst()){
mailid =Emails.getString(emailid);
}else {
}
} catch (Exception e) {
//failed
} finally {
if (cursor!=null) {
cursor.close();
}
}
}
}
}
}
Xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Trial2" />
</LinearLayout>
解决方案?请。
答案 0 :(得分:0)
使用此代码获取电子邮件
Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[]{id}, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
String email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
}
emailCur.close();
获取gmail id的值后,你必须将这些值传递给listview,在你的代码中,活动检查中没有listview一次......
答案 1 :(得分:0)
尝试使用此代码段在listview中显示您的电子邮件。这将有所帮助
Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[]{id}, null);
while (emailCur.moveToNext()) {
String email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
} emailCur.close();