Phone Contacts Display in Listview using Adapter and asynctask?

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();
    }
});
  1.   class LoadContactsAyscn extends AsyncTask<Void, Void, ArrayList<Contacts_Item>> {
    ProgressDialog progressdialog;

    @Override
    protected void onPreExecute() {
    super.onPreExecute();
    progressdialog = ProgressDialog.show(Contact1.this, "Loading Contacts", "Please Wait");
    }

    @Override
    protected 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;
    }

    @Override
    protected 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() {
    @Override
    public 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


  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:orientation="vertical" >

  7.     <LinearLayout
  8.         android:id="@+id/linearLayout1"
  9.         android:layout_width="match_parent"
  10.         android:layout_height="50dp"
  11.         android:layout_alignParentLeft="true"
  12.         android:layout_alignParentRight="true"
  13.         android:background="@drawable/bar" >

  14.         <TextView
  15.             android:id="@+id/contacts"
  16.             android:layout_width="match_parent"
  17.             android:layout_height="wrap_content"
  18.             android:layout_gravity="center"
  19.             android:gravity="center"
  20.             android:text="Contacts"
  21.             android:textColor="#FFFFFF"
  22.             android:textSize="30sp"
  23.             android:textStyle="bold" />
  24.     </LinearLayout>

  25.     <ListView
  26.         android:id="@+id/listView1"
  27.         android:layout_width="match_parent"
  28.         android:layout_height="wrap_content"
  29.         android:divider="#000000"
  30.         android:dividerHeight="2px" >
  31.     </ListView>

  32. </LinearLayout>

MySimpleArrayAdapter.java 



  1. public class MySimpleArrayAdapter extends ArrayAdapter<Contacts_Item> {
  2.   private final Context context;
  3.   private final ArrayList<Contacts_Item> values;  

  4.   public MySimpleArrayAdapter(Context context, int custom, ArrayList<Contacts_Item> news_list) {
  5.  super(context, R.layout.adapter, news_list);
  6.    this.context = context;
  7.  this.values = news_list;
  8.   }
  9.   
  10.   @Override
  11.   public View getView(int position, View convertView, ViewGroup parent) {  
  12.  if(convertView == null) {
  13.  LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  14.  convertView = inflater.inflate(R.layout.adapter, parent, false);    
  15.  }  
  16.  
  17.  TextView textView = (TextView) convertView.findViewById(R.id.name);
  18.  TextView textView1 = (TextView) convertView.findViewById(R.id.number);
  19.  
  20.  Contacts_Item acc_info = new Contacts_Item();
  21.  acc_info = values.get(position);  
  22.    
  23.  String name = acc_info.getName();  
  24.  String number = acc_info.getType();
  25.  textView.setText(name);
  26.  textView1.setText(number);  
  27.  
  28.  convertView.setTag(acc_info);   
  29.  return convertView;
  30.   }

Here is the class for we can set and get the values.

 Contacts_Item.java 


  1. public class Contacts_Item {
  2.  
  3. private String Name;
  4. private String Number;

  5. public String getType() {
  6. return Number;
  7. }
  8. public void setType(String type) {
  9. this.Number = type;
  10. }  
  11. public String getName() {
  12. return Name;
  13. }
  14. public void setName(String name) {
  15. Name = name;
  16. }


Output


No comments:

Select DateRange UsingRangePicker.

  /* * This Method is for select range from picker. * */ private fun selectDateRangeUsingRangePicker () { pageNumber = 1 val displ...