I am working on an application in which i need to fetch all the contacts from the contact book and display. i want the user to select some contacts and add them in a group which is saved in db.
i have created a custom list view- contactitem.xml-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/contactname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:layout_marginLeft="20dp"
android:ellipsize="end"
android:singleLine="true"
android:clickable="true"/>
<TextView
android:id="@+id/contactnum"
android:layout_width="wrap_content"
android:textColor="@color/White"
android:clickable="true"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="@string/add_contacts"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:focusable="false"
android:focusableInTouchMode="false"/>
</LinearLayout>
i have a SelectContact class for fetching contacts from Contact book-
public class SelectContacts extends Activity implementsOnItemClickListener {
private List<Contact> list = new ArrayList<Contact>();
private ListView contact_list;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selectcontacts);
contact_list=(ListView)findViewById(R.id.contactsListView);
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
cur.moveToFirst();
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 (Integer.parseInt(cur.getString(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);
pCur.moveToFirst();
while (pCur.moveToNext()) {
String number=pCur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Contact c=new Contact();
c.setContactName(name);
c.setContactNum(number);
list.add(c);
}
pCur.close();
}
ContactAdapter contactAdapter=new ContactAdapter(this, R.layout.contactitem, list);
contact_list.setAdapter(contactAdapter);
}
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
}
and implemented a custom adapter- ContactAdapter-
public class ContactAdapter extends ArrayAdapter<Contact>{
List<Contact> items;
LayoutInflater mInflater ;
Context context;
int layoutResourceId;
public ContactAdapter(Context context, int layoutResourceId, List<Contact> items) {
super(context, layoutResourceId, items);
this.layoutResourceId=layoutResourceId;
this.items = items;
this.context=context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = new ViewHolder();
if(row==null){
mInflater = ((Activity)context).getLayoutInflater();
row = mInflater.inflate(layoutResourceId, parent, false);
holder.name = (TextView) row.findViewById(R.id.contactname);
holder.number = (TextView) row.findViewById(R.id.contactnum);
holder.add=(Button)row.findViewById(R.id.add);
row.setTag(holder);
}else{
holder=(ViewHolder)row.getTag();
}
String name=items.get(position).getContactName();
String number=items.get(position).getContactNum();
holder.name.setText(name);
holder.number.setText(number);
holder.add.setText("Add");
return row;
}
static class ViewHolder{
TextView name;
TextView number;
Button add;
}
}
here Contact is a simple POJO-
public class Contact {
private String contactName;
private String contactNum;
public String getContactName() {
return contactName;
}
public void setContactName(String contactName) {
this.contactName = contactName;
}
public String getContactNum() {
return contactNum;
}
public void setContactNum(String contactNum) {
this.contactNum = contactName;
}
}
i am a newbie in android..
Here when i start SelectContact activity it cannot display contact on the UI.. please help me guys...
Thanks
To add rows to a ListView you need to add it to your layout and implement an IListAdapter with methods that the ListView calls to populate itself. Android includes built-in ListActivity and ArrayAdapter classes that you can use without defining any custom layout XML or code.
This example demonstrates how do I dynamically update a ListView in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
I will insist you to use List<Contact> list = new ArrayList<Contact>();
instead of using ArrayList<HashMap<String, Contact>> list= new ArrayList<HashMap<String, Contact>>();
You just need to fetch the Contact details and set the values to the Contact
Class and add to the ArrayList
object. And then just pass the list Object to ArrayAdapter class and use it. This is the easiest way, you are making it a bit complex using HashMap
in this case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With