Android游标返回空联系人

时间:2016-04-27 06:51:50

标签: android android-contacts android-cursoradapter

我需要获得至少拥有电话号码的所有联系人。 Android联系人可以从许多帐户中挑选,例如gmail,skype,vibe等。我制作了我需要的课程以获得我需要的联系人。我的问题是,无论如何,联系人不会拥有至少1个电话号码,只显示他们的姓名和头像。任何人都可以在我的代码中说出我做错了吗?我的代码源在下面分享。

ContactsActivity.class

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    selectionString = edtSearch.getText().toString();
    String[] selectionArgs = {"%" + selectionString + "%", selectionString + "%", "%" + selectionString};
    String selection = ContactsContract.Contacts.DISPLAY_NAME + " LIKE ? OR "
            + ContactsContract.Contacts.DISPLAY_NAME + " LIKE ? OR "
            + ContactsContract.Contacts.DISPLAY_NAME + " LIKE ? AND "
            + ContactsContract.Contacts.HAS_PHONE_NUMBER + "=='1'";

    return new CursorLoader(this,
            ContactsContract.Contacts.CONTENT_URI, // URI
            null, // projection fields
            selection, // the selection criteria
            selectionArgs, // the selection args
            ContactsContract.Contacts.DISPLAY_NAME + " ASC" // the sort order
    );
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    ContactsAdapter.createCheckedContacts(data.getCount());
    contactsAdapter.setCursor(data);
    contactsAdapter.swapCursor(data);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    contactsAdapter.swapCursor(null);
}

ContactsAdapter.class

public class ContactsAdapter extends CursorAdapter {

    static boolean status = true;
    private static boolean[] checkedContacts;
    private static Context context;
    private static Cursor cursor;

    public ContactsAdapter(Context context, Cursor c) {
        super(context, c, 0);
        this.context = context;
    }

    public static void setCursor(Cursor cursor) {
        ContactsAdapter.cursor = cursor;
    }

    public static void createCheckedContacts(int count) {
        checkedContacts = new boolean[count];
    }

    public static void saveSelectedContacts(ClientDao contactDao) {
        for (int i = 0; i < checkedContacts.length; i++) {
            if (checkedContacts[i]) {
                cursor.moveToPosition(i);
                DatabaseHelper.getInstance().saveClient(ContactUtils.cursorToContact(cursor, context), status);
                status = !status;
            }
        }
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View convertView = ((Activity) context).getLayoutInflater().inflate(R.layout.contact_item, parent, false);
        ViewHolder holder = new ViewHolder(convertView);
        convertView.setTag(holder);
        return convertView;
    }

    @Override
    public void bindView(View convertView, final Context context, final Cursor cursor) {
        final ViewHolder holder = (ViewHolder) convertView.getTag();
        final Contact contact = ContactUtils.cursorToContact(cursor, context);
        holder.tvName.setText(contact.getDisplayName());

        if (isFirst(cursor)) {
            holder.tvLetter.setVisibility(View.VISIBLE);
            String letter = String.valueOf(Character.toUpperCase(contact.getDisplayName().charAt(0)));
            holder.tvLetter.setText(letter);
        } else {
            holder.tvLetter.setVisibility(View.INVISIBLE);
        }
        convertView.setTag(convertView.getId(), contact);
        if (!TextUtils.isEmpty(contact.getPhotoUri())) {
            new ImageLoaderUtils.ContactImage(contact, convertView, holder.ivUserImage, context).execute();
        } else {
            holder.ivUserImage.setImageResource(R.drawable.ic_profile);
        }

        holder.inviteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ContactInvitationActivity_.class);
                intent.putExtra("contact", contact);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                context.startActivity(intent);
            }
        });
    }

    private boolean isFirst(Cursor cursor) {
        Contact c1 = ContactUtils.cursorToContact(cursor, context);
        if (cursor.getPosition() == 0)
            return true;

        cursor.moveToPrevious();
        Contact c2 = ContactUtils.cursorToContact(cursor, context);
        if (c1.getDisplayName() == null || c2.getDisplayName() == null)
            return false;
        if (Character.toUpperCase(c1.getDisplayName().charAt(0)) != Character.toUpperCase(c2.getDisplayName().charAt(0)))
            return true;

        return false;
    }

    private static class ViewHolder {
        TextView tvName;
        TextView tvLetter;
        ImageView ivUserImage;
        Button inviteBtn;
        RelativeLayout rlContact;

        private ViewHolder(View convertView) {
            tvName = (TextView) convertView.findViewById(R.id.tvContactsName);
            tvLetter = (TextView) convertView.findViewById(R.id.tvLetter);
            ivUserImage = (ImageView) convertView.findViewById(R.id.ivUserIcon);
            inviteBtn = (Button) convertView.findViewById(R.id.inviteBtn);
            rlContact = (RelativeLayout) convertView.findViewById(R.id.rlContact);
        }
    }
}

ContactUtil.class

public class ContactUtils {

    public static Contact cursorToContact(Cursor c, Context context) {

        if (c == null || c.getPosition() < 0) return null;
        Contact contactObj = new Contact();

        try {
            contactObj.setID(c.getString(c.getColumnIndex(ContactsContract.Contacts._ID)));
            contactObj.setDisplayName(c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
            contactObj.setPhotoUri(c.getString(c.getColumnIndex(ContactsContract.Contacts.PHOTO_URI)));
            contactObj.setPhoneNumber("");

            contactObj.setEmail("");
            setPhoneNumber(c, contactObj, context);
            setEmail(contactObj, context);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return contactObj;
    }

    public static void setPhoneNumber(Cursor c, Contact contactObj, Context context) {
        if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            // Query phone here. Covered next
            Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactObj.getID(), null, null);
            phones.moveToFirst();
            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            Log.i("Number", phoneNumber);
            contactObj.setPhoneNumber(phoneNumber);
            phones.close();

        }
    }

    public static void setEmail(Contact contactObj, Context context) {
        Cursor emailCur = context.getContentResolver().query(
                ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                new String[]{contactObj.getID()}, 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));
            contactObj.setEmail(email);
        }
        emailCur.close();
    }
}

0 个答案:

没有答案