如何从手机获取联系人并在列表视图中显示按钮点击android?

时间:2017-02-01 11:25:41

标签: android listview

我希望从手机中获取所有联系人,只需在android ListView中显示,而无需在数据库中存储,目的是当用户点击按钮“同步所有联系人”时,应该获取所有联系人并显示在ListView。这是主要的活动类代码

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

这里是Contats_list类

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

}

这是Custom_adapter类

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;


 }
}

请告诉我如何获得联系人并在listview中显示我是新来的 任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

在清单中添加权限

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

取两个变量

String name, phonenumber ;

你有点击这里

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

您可以访问此类联系人。

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

        }

答案 1 :(得分:0)

我是这样做的。

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

          //Call the code written below
       }
   });

从移动设备获取联系人的安全代码。根据您的需要修改此代码:

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

答案 2 :(得分:0)

get android contact phone number list可能重复。在这个链接中,他们已经获取了联系号码列表,并且您可以获得联系人姓名列表