Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7012867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:18:42+00:00 2026-05-27T22:18:42+00:00

I am working on an application in which i need to fetch all the

  • 0

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 implements OnClickListener{

    private String groupName;
    private Button back;
    private Button home;
    private List<Contact> list = new ArrayList<Contact>();

    private ListView contactList;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.selectcontacts);

            Bundle bundle = getIntent().getExtras();
            groupName=bundle.getString("groupName");

            back=(Button)findViewById(R.id.back_button);
            back.setOnClickListener(this);

            home=(Button)findViewById(R.id.home_button);
            home.setOnClickListener(this);

            contactList=(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) {

                do{

                    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) {

                    String[] fields={ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER};    

                    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, fields, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);

                    pCur.moveToFirst();

                     do { 

                        String number=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                        Contact c=new Contact(name,number);

                        list.add(c);

                        }while (pCur.moveToNext());

                        pCur.close();
                    }               



                   }while (cur.moveToNext());

                ContactAdapter contactAdapter=new ContactAdapter(this, R.layout.contactitem, list, contactList,groupName);



                contactList.setAdapter(contactAdapter);


            }


        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent= new Intent();

            if(v==back){

                Bundle bundle = new Bundle();
                bundle.putString("groupName", groupName);
                intent.putExtras(bundle);

                intent.setClass(SelectContacts.this, GroupDetails.class);
                startActivity(intent);

            }
            if(v==home){
                intent.setClass(SelectContacts.this, Welcome.class);
                startActivity(intent);

            }

        }
}

and implemented a custom adapter- ContactAdapter-`

public class ContactAdapter extends ArrayAdapter<HashMap<String, Contact>>{

private ArrayList<HashMap<String, Contact>> items;

private LayoutInflater mInflater ; 

public ContactAdapter(Context context, int textViewResourceId, ArrayList<HashMap<String, Contact>> items) {

        super(context, textViewResourceId, items);

        this.items = items;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

            mInflater = LayoutInflater.from(getContext());

            ViewHolder holder;

       if(convertView==null){

            convertView = mInflater.inflate(R.layout.contactitem, parent, false);

            holder = new ViewHolder();

            holder.name = (TextView) convertView.findViewById(R.id.contactname);

            holder.number = (TextView) convertView.findViewById(R.id.contactnum);

            holder.add=(Button)convertView.findViewById(R.id.add);

            convertView.setTag(holder);

        }

       else{

            holder=(ViewHolder)convertView.getTag();

        }

       String name=items.get(position).get(""+position).getContactName();

       String number=items.get(position).get(""+position).getContactNum();

       holder.name.setText(name);

       holder.number.setText(number);

        return convertView;
}



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..

The above code works quiet fine on emulater and fetch contact name and numbers to display in list. but when i test this code on phone which have a Gmail account syncronized, it got crashed with following error..

CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

I don’t have any idea why this is happening. Please give some suggestions…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:18:43+00:00Added an answer on May 27, 2026 at 10:18 pm

    It seems you forgot to call cur.moveToFirst(); after fetching the Contact Cursor. After this line,

    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    

    You are also missing the same thing here, after this – pCur.moveToFirst();

    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an application in which i need to fetch all the
I am working on an application in which i need to store all the
I am working on application which needs to fetch values from database and pass
I'm working on an application which implements longpolling because I want the user to
I am currently working on an application in-which I need to take the screenshot
Hi I am working on an application in which I need almost 10 webBrowsers
I'm working on an asp.net application which uses Lucene.net I need to highlight the
I'm working on an application which has data imported from a foreign (and wholly
I have a working JNLP application which I need to distribute to various non-technical
I have a working application which gets the GPS Long/Lat of from a BlackBerry

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.