-2

I want to get 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 code

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

here is Contats_list class

      public class Contact_list {
private String name;
private String phone;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public Contact_list(String name, String phone) {
    this.name = name;
    this.phone = phone;
 }

}

Here is Custom_adapter class

public class Custom_adapter extends ArrayAdapter<Contact_list> {


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


//ArrayList<Contact_list> mylist;

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

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return persons.size();
}

@Override
public Contact_list 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 p) {

    if (convertView == null) {


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

        holder = new ViewHolder();
        holder. name = (TextView) convertView.findViewById(R.id.txt_name);
        holder. phone = (TextView) convertView.findViewById(R.id.txt_phone);
        convertView.setTag(holder);
    }else{
        //Get viewholder we already created
        holder = (ViewHolder)convertView.getTag();
    }
    Contact_list person = persons.get(position);
    if(person != null){


        holder.name.setText(person.getName());
       holder.phone.setText(person.getPhone());

    }
    return convertView;
}

private static class ViewHolder {
 TextView name;
 TextView phone;


 }
}

please tell me how i can get contacts and show in listview i am new here any help will be appreciated.

user6405383
  • 146
  • 1
  • 2
  • 13

3 Answers3

0

Add permission in manifest

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

Take two variables

String name, phonenumber ;

you have your on click here

 sync.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
      GetContactsIntoArrayList();            
               }
           });

you can access contacts like this.

 public void GetContactsIntoArrayList(){

            cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null, null, null);

            while (cursor.moveToNext()) {

                name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

                phonenumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                StoreContacts.add(name + " "  + ":" + " " + phonenumber);
            }

            cursor.close();

        }
santosh kumar
  • 2,952
  • 1
  • 15
  • 27
0

Here is how I do it.

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

          //Call the code written below
       }
   });

A safe code for fetching contacts from mobile. Modify this code according to your need :

   List<ObjectContacts> list
   ObjectContacts recyclerItem;
   try{

        Toast.makeText(getApplicationContext(), "Starting fetch", Toast.LENGTH_SHORT).show();

        new AsyncTask<String, String, String>(){
            @Override
            protected String doInBackground(String... params){

              try{
                  ContentResolver cr = getContentResolver();
                  Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                          null, null, null, null);

                  if (cur.getCount() > 0) {
                      while (cur.moveToNext()) {
                          String id = cur.getString(
                                  cur.getColumnIndex(ContactsContract.Contacts._ID));
                          String name = cur.getString(cur.getColumnIndex(
                                  ContactsContract.Contacts.DISPLAY_NAME));

                          if (cur.getInt(cur.getColumnIndex(
                                  ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
                              Cursor pCur = cr.query(
                                      ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                      null,
                                      ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                                      new String[]{id}, null);
                              while (pCur.moveToNext()) {
                                  String phoneNo = pCur.getString(pCur.getColumnIndex(
                                          ContactsContract.CommonDataKinds.Phone.NUMBER));
                                  recyclerItem = new ObjectContacts();
                                  recyclerItem.setName(name);
                                  recyclerItem.setMobile(phoneNo);
                                  list.add(recyclerItem);
                              }
                              pCur.close();
                          }
                      }
                  }
                  cur.close();
              } catch (Exception e){
                  e.printStackTrace();
                  return "error";
              }
            }
            @Override
            public void onPostExecute(String data){
                try{
                    switch (data){
                        case "added":
                            //Contacts are now in your arrayList.
                            break;
                        case "error":
                            //Error occurred
                            break;
                        case "empty":
                            //No contacts found
                            break;
                    }
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        }.execute();

    } catch (Exception e){
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
    }
Basu
  • 763
  • 8
  • 27
0

possible duplicate of get android contact phone number list. In this link they had fetched the list of contact number,insted you can get contact name list

Community
  • 1
  • 1
vimal raj
  • 295
  • 1
  • 13