In this code I keep getting an error, I know its how I’m saving the contact to the database. because if I take out the cdata/cdata2 and just put a string it works fine. Any help will be appreciated.
How do I convert this to accept the string from the contact?
switch (item.getItemId()) {
case 1:
//Save current contact to stash database
Cursor phones2 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null, null);
String cdata = phones2.getString(phones2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String cdata2 = phones2.getString(phones2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
ContentValues values=new ContentValues(2);
values.put(DatabaseHelper.NAME, cdata);
values.put(DatabaseHelper.cALUE, cdata2);
db.getWritableDatabase().insert("constants", DatabaseHelper.NAME, values);
constantsCursor.requery();
//Refresh contact list
Intent refresh = new Intent(this, MainTab.class);
startActivity(refresh);
this.finish();
return(true);
case 2:
//uncoded option
Toast.makeText(this, "here is the info", Toast.LENGTH_SHORT).show();
}
ERROR:
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 2
You forgot to call Cursor.moveToFirst(), without this call the cursor will not be ready to be read from.
Try like this