Click here to Skip to main content
16,016,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi ,
Im trying to get the contact details but im getting error
couldnt read row 0 col -1 make sure cursor is initialized correclty.


following is my code :


<pre lang="java"><pre>try{
			
			final String[] projection = new String[] {
					RawContacts.CONTACT_ID,	// the contact id column
					//RawContacts.DELETED		// column if this contact is deleted
					};
			
			
			
		
			 Cursor rawContacts = getContentResolver().query(RawContacts.CONTENT_URI,projection , null,	null , null);
			List<HashMap<String, String>> map = new ArrayList<HashMap<String,String>>();
			 List<String> a = new ArrayList<String>();
			 //rawContacts.moveToPosition(2);
			if(rawContacts.moveToNext()) {
				final String name = rawContacts.getString(rawContacts.getColumnIndex(Contacts.DISPLAY_NAME));
					final String nmbr = rawContacts.getString(rawContacts.getColumnIndex(Contacts.HAS_PHONE_NUMBER));
					con = new HashMap<String, String>();
					con.put(NAME, nmbr);
					map.add(con);
					a.add(name);
				}
			rawContacts.close();
			ListView lst = (ListView) findViewById(R.id.lstContacts);
			SimpleAdapter ad = new SimpleAdapter(this,map,R.layout.activity_contacts, null,null);
			
			
			lst.setAdapter(ad);		
		}
		catch(Exception ex)
		{
			Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show();
		}
Posted

1 solution

Hello,

According to documentation youi can retrieve all contacts using following code.
Java
Cursor c = getContentResolver().query(RawContacts.CONTENT_URI, 
                new String[]{RawContacts._ID}, 
                RawContacts.CONTACT_ID + "=?", 
                new String[]{String.valueOf(contactId)}, null);

And the best way to read a raw contact along with all the data associated with it is by using the ContactsContract.RawContacts.Entity directory. More info[^].

For your problem change projection declaration as
Java
final String[] projection = new String[] {RawContacts.CONTACT_ID};

Regards,
 
Share this answer
 
v2
Comments
shanalikhan 8-Apr-13 11:30am    
i tried to change it , but same error im facing....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900