0

I'd like to add my selected PhonesContacts(currently 2 strings) to a listview. Currently im doing this: (Name and PhoneNo are written to the textview) This are the strings that i'd like to display in a list.

Its working with textView already but when i want to add a second one it overwrites the first one. Thats why i'd like to show it in a list.

How can i change this to listview?

I tried creating an ArrayList and passing the string to this ArrayList but this hasn't been working.

private void contactPicked(Intent data) {
    Cursor cursor = null;
    try {
        String phoneNo = null ;
        String name = null;
        // getData() method will have the Content Uri of the selected contact
        Uri uri = data.getData();
        //Query the content uri
        cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        // column index of the phone number
        int  phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        // column index of the contact name
        int  nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        phoneNo = cursor.getString(phoneIndex);
        name = cursor.getString(nameIndex);
        // Set the value to the textviews
        textView1.setText(name);
        textView2.setText(phoneNo);

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

For the listview i created a

private ListView listView;

and used it in my OnCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact_view);
    listView = (ListView) findViewById(R.id.listView);
bfmv991
  • 109
  • 1
  • 12

2 Answers2

2
private void contactPicked() {
    Cursor cursor = null;
ArrayList<String> phonenumberList = new ArrayList<String>(); // Declare ArrayList here
ArrayList<String> nameList = new ArrayList<String>(); // Declare ArrayList here
    try {
        String phoneNo = null ;
        String name = null;
        // getData() method will have the Content Uri of the selected contact

        cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        cursor.moveToFirst();
        // column index of the phone number
        int  phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        // column index of the contact name
        int  nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        phoneNo = cursor.getString(phoneIndex);
        name = cursor.getString(nameIndex);
        // Set the value to the textviews
        textView1.setText(name);
        textView2.setText(phoneNo);

        phonenumberList.add(phoneNo);  // add value in arraylist
        nameList.add(name);  // add value in arraylist


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

Set adapter in listview :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact_view);
    listView = (ListView) findViewById(R.id.listView);

    contactPicked();

  ArrayAdapter<String> arrayAdapter =      
                 new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, nameList); // Here Set your custom adapter which have two textview .
                 // Set The Adapter
                 listView.setAdapter(arrayAdapter); 

How to create custom adapter see below link :

Custom Adapter for List View

Community
  • 1
  • 1
KishuDroid
  • 5,411
  • 4
  • 30
  • 47
  • Thanks i'll try this out. Can you tell me what i shoud write into "yourdata"? my data intent? And simple_list_item1 is this my list that i created inside my xml? same for animalList – bfmv991 Oct 21 '15 at 09:14
  • 1
    You dont need to pass anything , just make contactPicked() method instead of contactPicked(Intent data). and call it as contactPicked() in onCreate() – KishuDroid Oct 21 '15 at 09:34
  • for what do you write contactPicked(yourdata); ?? and what do you mean with simple list item 1 and animallist which list should i use in there – bfmv991 Oct 21 '15 at 13:09
  • if i remove the intent data in contactPicked my code inside wont work properly cause i use it inside Uri uri = data.getData(); for example – bfmv991 Oct 21 '15 at 13:14
  • i have removed it and put proper query uri – KishuDroid Oct 21 '15 at 13:16
  • Ok i added your changes thanks for this. Now i'd need to know for what simple_list_item_1 and nameList stands for? Are theese two list created in the Adapter? – bfmv991 Oct 21 '15 at 13:17
  • Dude once study adapter and custom adapter.You will automatically get your answer. – KishuDroid Oct 21 '15 at 13:20
  • Okay thats why im asking i never used adapters before sry bro – bfmv991 Oct 21 '15 at 13:21
0

Try this

public class ContactsUtils {

public static ArrayList<ContactModel> getContactList(Context ctx) {

    ArrayList<ContactModel> list = new ArrayList<ContactModel>();
    String displayName = "", phoneNo = "", email = " ";
    ContentResolver contentResolver = ctx.getContentResolver();
    Cursor curMain = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (curMain.getCount() > 0) {
        while (curMain.moveToNext()) {
            String contactId = curMain.getString(curMain.getColumnIndex(ContactsContract.Contacts._ID));
            displayName = curMain.getString(curMain.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            Cursor phoneCursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{contactId}, null);
            contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{contactId}, null);
            Cursor emailCursor = contentResolver.query(Email.CONTENT_URI, null,Email.CONTACT_ID + " = " + contactId, null, null);
            if(emailCursor.getCount()>0){
                while (emailCursor.moveToNext()) {
                    email =   emailCursor.getString(emailCursor.getColumnIndex(Email.DATA));
                }
            }else{
                email = "";
            }
            emailCursor.close();
            if(phoneCursor.getCount()>0){
                while (phoneCursor.moveToNext()) {
                    phoneNo = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }
            }else{
                phoneNo = "";
            }
            phoneCursor.close();

//              if(!email.equals("")) {
                ContactModel cm = new ContactModel();
                cm.setDisplayName(displayName);
                cm.setPhoneNumber(phoneNo);
                cm.setEmail(email);
                list.add(cm);
//              }
        }
    }
     curMain.close();
    return list;
}
}

Then Create Contact ModelClass

public static class ContactModel implements Comparable<ContactModel>{
    private String displayName = "", phoneNumber = "", email = "";



    @Override
    public int compareTo(ContactModel another) {
        return getDisplayName().compareTo(another.getDisplayName());    
    }


    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }


    public ContactModel() {
    }

    public ContactModel(String name) {
        this.displayName = name;
    }

    public String toString() {
        return displayName;
    }
}

And than in your Activity oncreate

ArrayList<ContactModel> contactlist = new Arralist<>();
contactlist = ContactsUtils.getContactList(MainActivity.this);

Than pass this arrsylist in your Listview Adapter.

Narendra Motwani
  • 1,085
  • 10
  • 20