android - Getting Cursor IndexOutOfBound Exception when passing contact-id to fetch details from Contacts -
i getting contact details of person according account name working fine exception contacts have name not numbers. getting contact ids when fetch details using contact_id phone cursor causes exception
private void displayallcontactsbytype(string accountname) //account-name: mobikwik { cursor rawcursor = null; rawcursor = cresolver.query( contactscontract.rawcontacts.content_uri, new string[]{contactscontract.rawcontacts.contact_id}, contactscontract.rawcontacts.account_name + "= ? , "+contactscontract.rawcontacts.contact_id + " != 0", new string[]{accountname}, contactscontract.contacts.display_name_primary + " collate localized asc"); int contactidcolumn = rawcursor.getcolumnindex(contactscontract.rawcontacts.contact_id); int rawcursorcount = rawcursor.getcount(); int total = 1; utils.log("raw size", " " + rawcursorcount); while (rawcursor.movetonext()) { contactid = rawcursor.getlong(contactidcolumn); storecontactdetails(contactid ); } rawcursor.close(); }
from above methods getting contacts id used data in below method.
private void storecontactdetails( long contactid) //e.g contact_id-2512 { cursor phones = null; string[] projection = new string[]{ contactscontract.commondatakinds.phone.display_name, contactscontract.commondatakinds.phone.number, contactscontract.contacts.lookup_key, phone.has_phone_number, phone.type, phone.last_time_used }; phones = cresolver.query(phone.content_uri, projection, phone.contact_id + " = ? ", new string[]{string.valueof(contactid)}, null); phones.movetofirst(); getresultsfromphonecursor(phones); } public void getresultsfromphonecursor(cursor phones) { string email_id = ""; string contacttype = ""; string lasttimeused = ""; hashmap<string,string> contacttypewithnumber=new hashmap<>(); try { string hasphone = ""; display_name = ""; phonenumber = ""; display_name = phones.getstring(phones.getcolumnindex(contactscontract.commondatakinds.phone.display_name));// line causing exception here lasttimeused = convertlongtodateformat(phones.getstring(phones.getcolumnindex(phone.last_time_used))); hasphone = phones.getstring(phones.getcolumnindex(contactscontract.commondatakinds.phone.has_phone_number)); if (hasphone.equalsignorecase("1")) hasphone = "true"; else hasphone = "false"; if (boolean.parseboolean(hasphone)) { string lookupkey = phones.getstring(phones.getcolumnindex(contactscontract.contacts.lookup_key)); cursor emails = cresolver.query(contactscontract.commondatakinds.email.content_uri, new string[]{contactscontract.commondatakinds.email.data}, contactscontract.commondatakinds.email.contact_id + " = " + contactid, null, null); while (emails.movetonext()) { email_id = emails.getstring(emails .getcolumnindex(contactscontract.commondatakinds.email.data)); } emails.close(); { phonenumber = phones.getstring(phones.getcolumnindex(contactscontract.commondatakinds.phone.number)); int type = phones.getint(phones.getcolumnindex(phone.type)); switch (type) { case 0: contacttype = "whatsapp"; break; case phone.type_home: contacttype = "home"; break; case phone.type_mobile: contacttype = "mobile"; break; case phone.type_work: contacttype = "work"; break; case phone.type_other: contacttype = "other"; break; case phone.type_work_pager: contacttype = "pager"; break; case phone.type_mms: contacttype = "mms"; break; case phone.type_main: contacttype = "main"; break; case phone.type_fax_home: contacttype = "fax"; break; case phone.type_fax_work: contacttype = "fax-home"; break; } if(contactlist.contains(new contactswrapper(contactid, display_name, lookupkey, false, email_id, lasttimeused,null))) { try { hashmap<string,string>contacttypemapprevious=contactlist.get(contactlist.size()-1).getcontacttypewithnumber(); if (!contacttypemapprevious.get(contacttype).replace(" ","").equals(this.phonenumber.replace(" ",""))) { contacttypewithnumber.put(contacttype, phonenumber); } else { if(!contacttypewithnumber.containskey(contacttype)&& contacttypemapprevious.size()<=1) contacttypewithnumber.put(contacttype, phonenumber); } } catch (exception e) { e.printstacktrace(); } } else contacttypewithnumber.put(contacttype,phonenumber); } while (phones.movetonext()); contactlist.add(new contactswrapper(contactid, display_name, lookupkey, false, email_id, lasttimeused,contacttypewithnumber)); phones.close(); } } catch (exception e) { e.printstacktrace();//cursor out of bound exception here } }
why getting exception if have contact id not 0 or null. code works contacts have both name , numbers please in advance
wiki
Comments
Post a Comment