-3

I try to check if contact is favorite(starred) in Android. Here what I do:

String starred = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.STARRED));
            Boolean isFavorite;
            if(starred=="1"){
                isFavorite = true;
            }else{
                isFavorite = false;
            }

My isFavorite always returns false, even if starred returns 1. What is wrong with my code?

Siranush
  • 1
  • 1

2 Answers2

2
 public void checkFavorite(){

    Cursor getContactNumber = getContentResolver().query(
            ContactsContract.Data.CONTENT_URI,
            null,
            ContactsContract.CommonDataKinds.Phone.NUMBER+" LIKE ?",
            new String[] { "%"+number }, //number is your number to check
            null);

    if(getContactNumber != null) {

        while (getContactNumber.moveToNext()) {
            String favcheck = getContactNumber.getString(getContactNumber.getColumnIndex(ContactsContract.CommonDataKinds.Phone.STARRED));
        }

       if(favcheck.equals("1")){
        // contact is fav
       }else{
        // contact is not fav
        }
    }
}
  • Merely posting some code is not a satisfactory answer on StackOverflow. You should at least explain in a few sentences how your solution addresses the question. Further, seeing as this is an old question, you should further detail how your answer differs from the other answer / what makes your answer better in particular situation etc. – Patrick Oct 26 '21 at 10:35
0

You need to check your result with equals method. If you do it with ==, you try to check if two variables assignment to the same object. And obviously "1" and starred are different objects.

Try this:

if ("1".equals(starred) {
Aleksandr Podkutin
  • 2,532
  • 1
  • 20
  • 31