bfmv991
bfmv991

Reputation: 109

String to ListView Android PhoneContacts

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);

Upvotes: 0

Views: 91

Answers (2)

KishuDroid
KishuDroid

Reputation: 5451

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

Upvotes: 2

Narendra Motwani
Narendra Motwani

Reputation: 1115

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.

Upvotes: 0

Related Questions