0

In an Android app I've got a couple contacts from my contacts list. They can be either emails, phone numbers, or even other things. I now want to check which type it is and bind specific actions to them.

For example, if it is a type vnd.android.cursor.item/email_v2, I want to send a POST message with just the email field, and if it is a type vnd.android.cursor.item/phone_v2 I want to send a POST message with just the phone field.

Any ideas how I could check this?

Gathios
  • 39
  • 4
kramer65
  • 50,427
  • 120
  • 308
  • 488
  • Are you looking for `instance of` ? `email_v2` is what ? – Suresh Atta Nov 27 '13 at 15:57
  • Those types are `String`s (or `Uri`s?) if I'm not mistaken and you can most likely simply [compare then](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). Please add a code snippet that shows how you get the type and what variable type it actually is / how you plan to use them. – zapl Nov 27 '13 at 16:10

2 Answers2

0

I guess the way to go would be using overloading:
You implement multiple methods with different input parameters but the same name, such as:

checkContact(email_v2 email){ do things with email }
checkContact(phone_v2 phone){ do things with phone }
checkContact(String s){do things with random string }

I think you get my point.
If you want a simple if-statement, though:

if (contact instanceof vnd.android.cursor.item/email_v2){ do send }
Lauri P
  • 178
  • 2
  • 10
  • That does not work in Java. The overloaded method is already selected at compiletime. – zapl Nov 27 '13 at 16:06
  • It works if you know the type at compile time but you can't use overloading to execute different methods based on the actual type at runtime: http://stackoverflow.com/questions/3883414/is-there-any-reason-that-java-uses-late-static-binding-for-overloaded-methods-in (`catch` blocks do that though) – zapl Nov 27 '13 at 16:12
  • @zapl Alright, thanks, I see your point now :) -- I'm very new to Java myself (only worked with it less than a week) – Lauri P Nov 27 '13 at 16:16
0

You could try checking the class constant CONTENT_ITEM_TYPE for your different types, something like:

contact.CONTENT_ITEM_TYPE.equals("vnd.android.cursor.item/email_v2");