Wednesday 24 July 2013

Reading Contacts in a List Android

Leave a Comment
In Android Application it is often required to read the contacts from the Phone Contact Book and perform some operation. Today we study how to read Contacts from your phone and display them in a list. To do the same, set up a project in Eclipse IDE.
Set up the Main Xml where the List populate:-
main.xml:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000" >
    </ListView>
</LinearLayout>
Now set up an another xml, how each row looks like
alluser_row.xml :-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativelay"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/tvname"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3dp"
        android:layout_marginTop="3dp"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="Title"
        android:textColor="#000000"
        android:textSize="17dp"
        android:textStyle="bold" >
    </TextView>
    <TextView
        android:id="@+id/tvphone"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvname"
        android:layout_marginLeft="3dp"
        android:layout_marginTop="10dp"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="Phone no"
        android:textColor="#000000"
        android:textSize="14dp" >
    </TextView>
</RelativeLayout>
Now switch to javacode
MainActivity.java:-
package com.arpit.listcontactreaderandroid;
import java.util.ArrayList;
import java.util.List;
import com.example.customlistcontactreader.R;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity implements {
    private ListView listView;
    private List<ContactModel> list = new ArrayList<ContactModel>();
    int count=0;                                 // count the number of contacts
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
   
   
        listView = (ListView) findViewById(R.id.list);
       
        Cursor phones = getContentResolver().query(              
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
                null, null);
        while (phones.moveToNext()) {
            count++;
           
            String name = phones
                    .getString(phones
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = phones
                    .getString(phones
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            ContactModel objContact = new ContactModel();       // creating an instance for contactmodel
            objContact.setName(name);
            objContact.setPhoneNo(phoneNumber);
            list.add(objContact);                                               // adding contacts into the list
        }
        phones.close();
        System.out.println(count);                                               // total numbers of contacts
   
        listView.setAdapter(new ContactCustomAdapter(list, this));   // setting listview with an adapter
       
       
    }
          
    }
}
ContactModel.java:-
// model
package com.arpit.listcontactreaderandroid;
public class ContactModel {
    private String name;
    private String phoneNo;

   
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPhoneNo() {
        return phoneNo;
    }
    public void setPhoneNo(String phoneNo) {
        this.phoneNo = phoneNo;
    }
   
    }
// setting up an adapter for the list
ContactCustomAdapter.java
package com.arpit.listcontactreaderandroid;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
public class ContactCustomAdapter extends BaseAdapter {
    private List<ContactBean> list;
    Context context;
   
    ContactCustomAdapter (List<ContactBean> list, Context context){
        this.list = list;
        this.context = context;
    }
   
   
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }
    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return list.get(arg0);
    }
    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v = convertView;
        if (v == null)
        {
           LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           v = vi.inflate(R.layout.alluser_row, null);
        }
          TextView contactName = (TextView)v.findViewById(R.id.tvname);
          TextView contactPhone = (TextView)v.findViewById(R.id.tvphone);
        
         
         ContactModel msg = list.get(position);
          contactName.setText(msg.getName());
          contactPhone.setText(msg.getPhoneNo());                  
                      
       return v;
    }

}

0 comments:

Post a Comment