【问题标题】:Get owner's email address with CursorLoader使用 CursorLoader 获取所有者的电子邮件地址
【发布时间】:2012-07-05 18:03:02
【问题描述】:

我正在尝试用所有者的电子邮件填充 ArrayList。我在一个片段中这样做。这是我的代码:

 public class ItemDetailFragment extends Fragment implements
        LoaderManager.LoaderCallbacks<Cursor> {

    ArrayList<String> emails = new ArrayList<String>();

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getLoaderManager().initLoader(0, null, this);

    }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
                View myFragmentView = inflater.inflate(R.layout.formal_email_layout,
                                container, false);
                return myFragmentView;
       }


 public Loader<Cursor> onCreateLoader(int id, Bundle arguments) {
            return new CursorLoader(getActivity(),
                    // Retrieve data rows for the device user's 'profile' contact.
                    Uri.withAppendedPath(
                            ContactsContract.Profile.CONTENT_URI,
                            ContactsContract.Contacts.Data.CONTENT_DIRECTORY),
                    ProfileQuery.PROJECTION,

                    // Select only email addresses.
                    ContactsContract.Contacts.Data.MIMETYPE + " = ?",
                    new String[]{ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE},

                    // Show primary email addresses first. Note that there won't be
                    // a primary email address if the user hasn't specified one.
                    null);
        }

public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
            List<String> emails = new ArrayList<String>();
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                emails.add(cursor.getString(ProfileQuery.ADDRESS));
                // Potentially filter on ProfileQuery.IS_PRIMARY
                cursor.moveToNext();
            }

               Log.d("test", cursor.getString(ProfileQuery.ADDRESS));
            }


public void onLoaderReset(Loader<Cursor> cursorLoader) {
            }

            private interface ProfileQuery {
                String[] PROJECTION = {
                        ContactsContract.CommonDataKinds.Email.ADDRESS,
                        ContactsContract.CommonDataKinds.Email.IS_PRIMARY,
                };

                int ADDRESS = 0;
                int IS_PRIMARY = 1;
            }

    }

我基本上使用了here 给出的示例,我只是将它改编成一个片段。

当片段被午餐时,应用程序崩溃并且我得到这个错误:

07-05 12:27:55.235: W/dalvikvm(23217): threadid=1: thread exiting with uncaught exception (group=0x40c2f1f8)
07-05 12:27:55.235: E/AndroidRuntime(23217): FATAL EXCEPTION: main
07-05 12:27:55.235: E/AndroidRuntime(23217): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

(我的设备上有一个 Gmail 帐户)

【问题讨论】:

    标签: android email android-contacts android-cursorloader


    【解决方案1】:

    这是一个获取所有者电子邮件的小类。

    您只能使用获取帐户名称(谷歌同步电子邮件地址)的代码的第一部分。

    【讨论】:

      猜你喜欢
      • 2011-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-19
      • 2013-01-18
      • 2017-11-03
      • 2022-07-27
      相关资源
      最近更新 更多