Skip to content Skip to sidebar Skip to footer

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

Solution 1:

privatevoid contactPicked() {
    Cursor cursor = null;
ArrayList<String> phonenumberList = new ArrayList<String>(); // Declare ArrayList here
ArrayList<String> nameList = new ArrayList<String>(); // Declare ArrayList heretry {
        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 numberint  phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        // column index of the contact nameint  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 :

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

    contactPicked();

  ArrayAdapter<String> arrayAdapter =      
                 newArrayAdapter<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

Solution 2:

Try this

publicclassContactsUtils {

publicstatic ArrayList<ContactModel> getContactList(Context ctx) {

    ArrayList<ContactModel> list = newArrayList<ContactModel>();
    StringdisplayName="", phoneNo = "", email = " ";
    ContentResolvercontentResolver= ctx.getContentResolver();
    CursorcurMain= contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (curMain.getCount() > 0) {
        while (curMain.moveToNext()) {
            StringcontactId= curMain.getString(curMain.getColumnIndex(ContactsContract.Contacts._ID));
            displayName = curMain.getString(curMain.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            CursorphoneCursor= contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",newString[]{contactId}, null);
            contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", newString[]{contactId}, null);
            CursoremailCursor= 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("")) {ContactModelcm=newContactModel();
                cm.setDisplayName(displayName);
                cm.setPhoneNumber(phoneNo);
                cm.setEmail(email);
                list.add(cm);
//              }
        }
    }
     curMain.close();
    return list;
}
}

Then Create Contact ModelClass

publicstaticclassContactModelimplementsComparable<ContactModel>{
    privateString displayName = "", phoneNumber = "", email = "";



    @Overridepublic int compareTo(ContactModel another) {
        returngetDisplayName().compareTo(another.getDisplayName());    
    }


    publicStringgetEmail() {
        return email;
    }

    publicvoidsetEmail(String email) {
        this.email = email;
    }

    publicStringgetDisplayName() {
        return displayName;
    }

    publicvoidsetDisplayName(String displayName) {
        this.displayName = displayName;
    }

    publicStringgetPhoneNumber() {
        return phoneNumber;
    }

    publicvoidsetPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }


    publicContactModel() {
    }

    publicContactModel(String name) {
        this.displayName = name;
    }

    publicStringtoString() {
        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.

Post a Comment for "String To Listview Android Phonecontacts"