user6405383
user6405383

Reputation: 146

How to get all contacts from phone and display in listview on button click in android?

I want to fetch all Contacts from phone and simply display in android ListView, without store in database,the purpose is that when user click on button "sync all contacts" then ALL contacts should be fetched and show in ListView. Here is the main activity class i have also created getter-setter class with variable contacts_name and phone_no, and CustomAdapterclass

public class MainActivity extends AppCompatActivity {

ListView listView;
Button sync;



ArrayList<newlist> listitem;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listitem = new ArrayList<newlist>();
    listView = (ListView) findViewById(R.id.listViewID);
    registerForContextMenu(listView);

    sync= (Button) findViewById(R.id.syncID);
    sync.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

              //what method will be used here for sync contacts and display in listview
           }
       });
   }
}

please any help will be appreciated.

Upvotes: 1

Views: 2246

Answers (2)

Gaurav Chauhan
Gaurav Chauhan

Reputation: 376

Use below method to fetch contacts from phone's database(update the method according to your requirements, it currently fetches only contact numbers).

    List<Integer> fetchContacts() {
        final String[] projection = new String[]{
                ContactsContract.CommonDataKinds.Phone.NUMBER,
        };

        final Cursor phone = context.getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                projection, null, null, null);

        List<Integer> contacts = new ArrayList<>();
        while(phone.moveToNext()){
            contacts.add(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER););
        }
        phone.close();
        return contacts;
    }

After that, you can pass this list of contacts to the adapter and call notifyDataSetChanged() on adapter to reflect the changes in the listview.

Also, you need to declare uses-permission for contacts permission in the manifest file like this

<uses-permission android:name="android.permission.READ_CONTACTS"/>

Upvotes: 1

eminuluyol
eminuluyol

Reputation: 237

Try using this library, you can get all contacts easily.

https://github.com/EverythingMe/easy-content-providers

For the ListView, you need to write a custom adapter, you can also find a lot of examples on the internet. It is just a sample.

public class CustomListViewAdapter extends ArrayAdapter<Person> {

private final LayoutInflater inflater;
private final Context context;
private ViewHolder holder;
private final ArrayList<Person> persons;

public CustomListViewAdapter(Context context, ArrayList<Person> persons) {
    super(context,0, persons);
    this.context = context;
    this.persons = persons;
    inflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    return persons.size();
}

@Override
public Person getItem(int position) {
    return persons.get(position);
}

@Override
public long getItemId(int position) {
    return persons.get(position).hashCode();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {

        convertView = inflater.inflate(R.layout.list_view_item, null);

        holder = new ViewHolder();
        holder.personImage = (ImageView) convertView.findViewById(R.id.person_image);
        holder.personNameLabel = (TextView) convertView.findViewById(R.id.person_name_label);
        holder.personAddressLabel = (TextView) convertView.findViewById(R.id.person_address_label);
        convertView.setTag(holder);

    }
    else{
        //Get viewholder we already created
        holder = (ViewHolder)convertView.getTag();
    }

    Person person = persons.get(position);
    if(person != null){
        holder.personImage.setImageResource(person.getPhotoId());
        holder.personNameLabel.setText(person.getName());
        holder.personAddressLabel.setText(person.getAddress());

    }
    return convertView;
}

//View Holder Pattern for better performance
private static class ViewHolder {
    TextView personNameLabel;
    TextView personAddressLabel;
    ImageView personImage;

}
} 

In onCreate read the library's documentation and try to figure out how you can get contacts.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // GET CONTACTS DATA
    initialize();
}

private void initialize() {
    persons = new ArrayList<Person>();
    listView = (ListView) findViewById(R.id.person_list_view);
    listViewAdapter = new CustomListViewAdapter(MainActivity.this,persons);
    listView.setAdapter(listViewAdapter);
}

Upvotes: 1

Related Questions