0

i need some help with a contacts picker class that ive got. The class retrieves the contacts list and allows me to choose one, but when I go and choose another one, it just replaces the first one. I want to make a list of contacts in my app and not only one.

Thank you, Noam

The Code:

public static final int PICK_CONTACT = 1;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);

    Button btnPickContact = (Button) findViewById(R.id.btnPickContact);
    btnPickContact.setOnClickListener(new OnClickListener() {
        public void onClick(View _view) {
            Intent intent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
            startActivityForResult(intent, PICK_CONTACT);
        }
    });

}



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
    case (PICK_CONTACT): {
        if (resultCode == Activity.RESULT_OK) {
            Uri contentUri = data.getData();
            //Phone Name
            Cursor c = managedQuery(contentUri, null, null, null, null);
            c.moveToFirst();
            String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
            //Phone Number
            String contactId = contentUri.getLastPathSegment();
            Cursor cursor = getContentResolver().query(Phone.CONTENT_URI,
                    null, Phone._ID + "=?", new String[] { contactId },
                    null);// < - Note, not CONTACT_ID!
            startManagingCursor(cursor);
            Boolean numbersExist = cursor.moveToFirst();
            int phoneNumberColumnIndex = cursor
                    .getColumnIndex(Phone.NUMBER);
            String phoneNumber = "";
            while (numbersExist) {
                phoneNumber = cursor.getString(phoneNumberColumnIndex);
                phoneNumber = phoneNumber.trim();
                numbersExist = cursor.moveToNext();
            }
            stopManagingCursor(cursor);
            //Set
            TextView tv = (TextView) findViewById(R.id.txtSelContact);
            tv.setText(name + "-" + phoneNumber);
        }
        break;
    }
    }
}

}

And here is the on create function:

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);

    Button btnPickContact = (Button) findViewById(R.id.btnPickContact);
    btnPickContact.setOnClickListener(new View.OnClickListener() {
        public void onClick(View _view) {
            Intent intent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
            startActivityForResult(intent, PICK_CONTACT);
        }
    });

    //you may fill it here e.g. from your db
    contactList=new ArrayList<String>();

    arrayAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, contactList);

    final ListView lv = (ListView) findViewById(R.id.ContactListView);
    lv.setAdapter(arrayAdapter);

}

This is the layout.xml (for some reason it didn't let me post the code so i linked to an image) :

https://imagizer.imageshack.us/v2/516x255q90/4/igi5.png

Lines 29 - 35:

btnPickContact.setOnClickListener(new View.OnClickListener() {
        public void onClick(View _view) {
            Intent intent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
            startActivityForResult(intent, PICK_CONTACT);
        }
    });
Noam_I
  • 7
  • 3

1 Answers1

0

as far as I see you are writing results into the same textview:

//Set
TextView tv = (TextView) findViewById(R.id.txtSelContact);
tv.setText(name + "-" + phoneNumber);

You could specify a Listview in your Layout

<ListView
    android:id="@+id/ContactListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >
</ListView>

and use an arrayadapter to add your result to this Listview:

public class MainActivity extends Activity {
    public static final int PICK_CONTACT = 1;
    private ArrayList<String> contactList;
    private ArrayAdapter<String> arrayAdapter;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_main);

        Button btnPickContact = (Button) findViewById(R.id.btnPickContact);
        btnPickContact.setOnClickListener(new View.OnClickListener() {
            public void onClick(View _view) {
                Intent intent = new Intent(Intent.ACTION_PICK,
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
                startActivityForResult(intent, PICK_CONTACT);
            }
        });

        //you may fill it here e.g. from your db
        contactList=new ArrayList<String>();

        arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, contactList);

        final ListView lv = (ListView) findViewById(R.id.contactListView);
        lv.setAdapter(arrayAdapter);

    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
        case (PICK_CONTACT): {
            if (resultCode == Activity.RESULT_OK) {
                Uri contentUri = data.getData();
                //Phone Name
                Cursor c = managedQuery(contentUri, null, null, null, null);
                c.moveToFirst();
                String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                //Phone Number
                String contactId = contentUri.getLastPathSegment();
                Cursor cursor = getContentResolver().query(Phone.CONTENT_URI,
                        null, Phone._ID + "=?", new String[] { contactId },
                        null);// < - Note, not CONTACT_ID!
                startManagingCursor(cursor);
                Boolean numbersExist = cursor.moveToFirst();
                int phoneNumberColumnIndex = cursor
                        .getColumnIndex(Phone.NUMBER);
                String phoneNumber = "";
                while (numbersExist) {
                    phoneNumber = cursor.getString(phoneNumberColumnIndex);
                    phoneNumber = phoneNumber.trim();
                    numbersExist = cursor.moveToNext();
                }
                stopManagingCursor(cursor);
                //Set
                arrayAdapter.add(name + "-" + phoneNumber);
                arrayAdapter.notifyDataSetChanged();
            }
            break;
        }
        }
    }
}
Roman Pickl
  • 635
  • 8
  • 20
  • Thank you, It works. Another thing I really need to add here is the ability to call, send a message or email a contact from that activity in my app. How can I do that ? – Noam_I Jan 11 '14 at 10:42
  • Something is wrong, when I launch the activity by itself on a different library it works, but when i add it to my app's library and open the activity through the app, it crashes. What can I do ? – Noam_I Jan 11 '14 at 12:22
  • Here is the LogCat I get when it crashes: https://imagizer.imageshack.us/v2/1240x596q50/15/64j9.png – Noam_I Jan 11 '14 at 16:34
  • can you post your work_contacts.java on create function? – Roman Pickl Jan 11 '14 at 20:25
  • are the buttons and lists named correctly in your layout? can you post your layout xml as well? – Roman Pickl Jan 11 '14 at 21:44
  • linked up on main post. – Noam_I Jan 11 '14 at 22:13
  • any ideas for solving this ? – Noam_I Jan 12 '14 at 20:49
  • concerning sending sms / email look into SmsManager / http://stackoverflow.com/questions/2197741/how-to-send-email-from-my-android-application – Roman Pickl Jan 13 '14 at 06:05
  • added lines 29-35 on main post. – Noam_I Jan 13 '14 at 12:08
  • seems as if btnPickContact is null. have you double checked the names? as a next step i would comment out lines 29 - end of oncreate and try to start ist again. then proceed from there and consequently find the line that causes the error. – Roman Pickl Jan 13 '14 at 17:18
  • But I don't understand why the code works perfectly as an app itself, but when i add it to an app as an activity it crashes. You can't really find the error, I need to go through the whole code ? Thank you – Noam_I Jan 14 '14 at 17:43
  • sorry it is hard to help if everything works as intended with the provided code. – Roman Pickl Jan 15 '14 at 07:05
  • OK, I think I will keep working on that code, and then solve that problem. I am having a problem with writing a code that will allow a user to remove a contact from the list. do you have any idea ? – Noam_I Jan 17 '14 at 08:35
  • anyone with an idea ? – Noam_I Jan 18 '14 at 20:52