4

I have an array with some phoneNumbers, and I want to select and list device contacts which are also in my array.

Now I can only show the first phoneNumber that is also in the array.

Here is my code:

public class Contacts extends ListActivity {
    SharedPreferences settings ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.contacts);
        setTitle("Choose a phone");
        // Query: contacts with phone shorted by name
        settings = this.getSharedPreferences(SETTINGS, Context.MODE_PRIVATE);
        String message = settings.getString("contacts", "0,0");
        String [] contacts = message.split(",");

        String query = " IN (";
        for (int i = 1; i < contacts.length; i++) {
            query += contacts[i];
            if (i < contacts.length - 1)
                query += ",";
        }
        query += ")";


        Cursor mCursor = getContentResolver().query(
                Data.CONTENT_URI,
                new String[] { Data._ID, Data.DISPLAY_NAME, Phone.NUMBER, Phone.TYPE }, Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "' AND " + Phone.NUMBER + " IS NOT NULL AND " + Phone.NUMBER + query, null, Data.DISPLAY_NAME + " ASC");
        startManagingCursor(mCursor);

        // Setup the list
        ListAdapter adapter = new SimpleCursorAdapter(this, // context
                android.R.layout.simple_list_item_2, // Layout for the rows
                mCursor, // cursor
                new String[] { Data.DISPLAY_NAME, Phone.NUMBER }, // cursor
                // fields
                new int[] { android.R.id.text1, android.R.id.text2 } // view
        // fields
                );
        setListAdapter(adapter);
    }
Ry-
  • 218,210
  • 55
  • 464
  • 476
Ptotrix
  • 63
  • 5
  • 1
    you mean you need a checkbox to select the items from the list? Do you need to list all contacts from phone contacts? – Raghunandan May 25 '13 at 13:57
  • i need to list all contancts from phone if they are in my array too. – Ptotrix May 25 '13 at 15:15
  • if my contacts are: John 1 Sue 2 Dave 3 and my array: John 1 Sue 2 Caroline 4 The list must have 2 contacts: John 1 Sue 2 – Ptotrix May 25 '13 at 15:16
  • 1
    check this might help you http://stackoverflow.com/questions/16730064/get-android-contact-phone-number-list/16730173#16730173. get contacts from phone contacts – Raghunandan May 25 '13 at 15:17
  • Maybe im not explaining well, i need same contact list as Whatsapp, i can list all my contacts, name and number, but i want to list only those who have installed my application. In contacts[] are all the numbers registered in my application and i need to list those who appear in both, contacts[] and android contacts – Ptotrix May 25 '13 at 15:44
  • 1
    i am not aware of how to do that. but if you have a array of strings. it should easy to display it in a list. – Raghunandan May 25 '13 at 15:45
  • i want to list the intersection of both group of contacts, array and android contacts, not all of them. that's the problem for now i only can list one contact who exists in both group of contacts – Ptotrix May 25 '13 at 15:57

1 Answers1

1

I am not sure whether this is the only issue. You have not use the apostrophe in IN statemet

You can try using

String query = " IN (";
        for (int i = 1; i < contacts.length; i++) {
            query+= "'";
            query += contacts[i];
            query+= "'";
            if (i < contacts.length - 1)
                query += ",";
        }
        query += ")";
stinepike
  • 54,068
  • 14
  • 92
  • 112