0

我使用的代码将在列表视图中显示名称和编号,但我还需要显示电子邮件,为此我添加了一些代码片段,但电子邮件没有显示在列表视图中。我使用的是 Android 2.1 平台。

代码

 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>

解决方案 ?请。

4

2 回答 2

0

尝试使用此代码片段在列表视图中显示您的电子邮件。这将很有帮助

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();
于 2012-06-14T07:18:08.787 回答
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 一次......

于 2012-06-14T07:03:15.573 回答