5

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

joyBlanks
  • 6,419
  • 1
  • 22
  • 47
Ashish Kumar
  • 960
  • 2
  • 11
  • 20

1 Answers1

5

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.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • 1
    Bro i have changed the Code and but still getting nothing.. I debugged the SelectContact Class. Here it is fetching names but cann't retrieve numbers.. – Ashish Kumar Jan 04 '12 at 07:33
  • so you are getting the names in list but not the numbers, is it so? – Lalit Poptani Jan 04 '12 at 07:46
  • no.. i mean to say that i put a toast in the loop to popup the number.. bt it is not displaying any thing.. i think the flow is not reaching in the pCur Loop.. – Ashish Kumar Jan 04 '12 at 07:49
  • I checked your code you are having problem in fetching the Contact details and storing in the ArrayList, just check your while loops. – Lalit Poptani Jan 04 '12 at 08:08
  • thanks bro... I have managed loops.. now i can display both name and number.... :) – Ashish Kumar Jan 04 '12 at 09:07
  • @LalitPoptani i had fetched the contacts & i tried to select multiple contacts but whenever i scroll down or up it automatically deselect the selected contacts & when i deselect the selected contacts manually & scroll down or up it automatically get selected,....\ – Wolverine Mar 26 '12 at 05:24
  • This is the problem of Adapter class check [this one](http://stackoverflow.com/questions/7738527/getting-an-issue-while-checking-the-dynamically-generated-checkbox-through-list/7738854#7738854) – Lalit Poptani Mar 26 '12 at 05:30