user1198331
user1198331

Reputation: 139

How to use contact Contentprovider?

package com.Kiro.Test;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;

public class TestActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        setContentView(R.layout.main);
        Uri uri=People.CONTENT_URI;
        String projection[]=new String[]{People._ID,People.NAME,People.NUMBER};
        Cursor cur=this.managedQuery(uri, projection, null, null, null);
        int id=cur.getColumnIndex(People._ID);
        super.onCreate(savedInstanceState);
        do{
            System.out.print(cur.getString(id));
            System.out.println("");
        }while(cur.moveToNext());
    }

}

when I run this code in my emulator,the logcat find CursorIndexoutofBoundsException can you help me work out this problem??

Upvotes: 0

Views: 429

Answers (2)

jeet
jeet

Reputation: 29199

seems problem is here:

  do{
            System.out.print(cur.getString(id));
            System.out.println("");
        }while(cur.moveToNext());

you need to first call cur.moveToFirst() to set cursor on 0th Position:

cur.moveToFirst();
  while(cur.hasNext(){
            cur.moveToNext();
            System.out.print(cur.getString(id));
            System.out.println("");
        }

Upvotes: 1

deepak Sharma
deepak Sharma

Reputation: 1641

Instead of using the "do while" try with the while because do while run at least once even condition is false.

while(cur.moveToNext()){  
System.out.print(cur.getString(id));
System.out.println("");
}   

My be this was the problem.

Upvotes: 0

Related Questions