Today we discuss the one of the important topic is How to get the phone contacts,
We can get contacts using using this CURSOR and another topic is ContactsContract this two functions are very important to get the contacts.
Let's Start for coding.
MainActivity.java
Button button_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LoadContactsAyscn lca = new LoadContactsAyscn();
lca.execute();
}
});
- class LoadContactsAyscn extends AsyncTask<Void, Void, ArrayList<Contacts_Item>> {ProgressDialog progressdialog;@Overrideprotected void onPreExecute() {super.onPreExecute();progressdialog = ProgressDialog.show(Contact1.this, "Loading Contacts", "Please Wait");}@Overrideprotected ArrayList<Contacts_Item> doInBackground(Void... params) {Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);while (cursor.moveToNext()) {contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));phNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));Contacts_Item weather = new Contacts_Item();weather.setName(contactName);weather.setType(phNumber);contacts_list.add(weather);}cursor.close();return contacts_list;}@Overrideprotected void onPostExecute(final ArrayList<Contacts_Item> news_list) {super.onPostExecute(news_list);progressdialog.cancel();MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(Contact1.this, R.layout.adapter, news_list);listview.setAdapter(adapter);listview.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {Object ob = (Object) view.getTag();if(ob != null) {Contacts_Item Details = (Contacts_Item) view.getTag();String name = Details.getName();String contactno = Details.getType();Toast.makeText(Contact1.this, "position ==> " +position + name + " ==> "+contactno , Toast.LENGTH_SHORT).show();Intent intent = new Intent(Contact1.this, CustomerData1.class);Bundle bundle = new Bundle();bundle.putInt("position", position);intent.putExtra("contactname", name);intent.putExtra("phnumber", contactno);startActivity(intent);}}});}}
activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <LinearLayout
- android:id="@+id/linearLayout1"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:layout_alignParentLeft="true"
- android:layout_alignParentRight="true"
- android:background="@drawable/bar" >
- <TextView
- android:id="@+id/contacts"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:gravity="center"
- android:text="Contacts"
- android:textColor="#FFFFFF"
- android:textSize="30sp"
- android:textStyle="bold" />
- </LinearLayout>
- <ListView
- android:id="@+id/listView1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:divider="#000000"
- android:dividerHeight="2px" >
- </ListView>
- </LinearLayout>
MySimpleArrayAdapter.java
- public class MySimpleArrayAdapter extends ArrayAdapter<Contacts_Item> {
- private final Context context;
- private final ArrayList<Contacts_Item> values;
- public MySimpleArrayAdapter(Context context, int custom, ArrayList<Contacts_Item> news_list) {
- super(context, R.layout.adapter, news_list);
- this.context = context;
- this.values = news_list;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- if(convertView == null) {
- LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- convertView = inflater.inflate(R.layout.adapter, parent, false);
- }
- TextView textView = (TextView) convertView.findViewById(R.id.name);
- TextView textView1 = (TextView) convertView.findViewById(R.id.number);
- Contacts_Item acc_info = new Contacts_Item();
- acc_info = values.get(position);
- String name = acc_info.getName();
- String number = acc_info.getType();
- textView.setText(name);
- textView1.setText(number);
- convertView.setTag(acc_info);
- return convertView;
- }
- }
Here is the class for we can set and get the values.
Contacts_Item.java
Output
- public class Contacts_Item {
- private String Name;
- private String Number;
- public String getType() {
- return Number;
- }
- public void setType(String type) {
- this.Number = type;
- }
- public String getName() {
- return Name;
- }
- public void setName(String name) {
- Name = name;
- }
- }
Output