android - Loading contacts from phonebook causing the app to stop -
i trying fetch contacts phonebook , show them in listview. problem contacts have been fetched & showed in listview after few seconds app goint crashed, fetching contact list & fitting items in listview sucessfull app crashed after time. here mainactivity.java-
public class mainactivity extends listactivity { // arraylist<string> values = new arraylist<string>(); string[] values; context mcontext; string name; int j=0,c=0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); cursor phones = getcontentresolver().query(contactscontract.commondatakinds.phone.content_uri, null,null,null, null); while (phones.movetonext()) { name=phones.getstring(phones.getcolumnindex(contactscontract.commondatakinds.phone.display_name)); c=c+1; } phones.close(); values = new string[c]; fun(); myarrayadapter adapter = new myarrayadapter(this, values); setlistadapter(adapter); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } private void fun() { // todo auto-generated method stub cursor phones1 = getcontentresolver().query(contactscontract.commondatakinds.phone.content_uri, null,null,null, null); while (phones1.movetonext()) { name=phones1.getstring(phones1.getcolumnindex(contactscontract.commondatakinds.phone.display_name)); string number=phones1.getstring(phones1.getcolumnindex(contactscontract.commondatakinds.phone.number)); values[j]=name; j=j+1; } phones1.close(); } }
and myarrayadapter.java -
public class myarrayadapter extends arrayadapter<string> { context context; string[] values; public myarrayadapter(context context, string[] values) { super(context, r.layout.activity_main, values); // todo auto-generated constructor stub this.context = context; this.values = values; } @override public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); view v = inflater.inflate(r.layout.activity_main, parent, false); textview txtv = (textview) v.findviewbyid(r.id.member_name); textview txtv1 = (textview) v.findviewbyid(r.id.status); imageview imgv = (imageview)v.findviewbyid(r.id.profile_pic); bitmap icon = bitmapfactory.decoderesource(context.getresources(), r.drawable.ok); for(int pos = 0; pos<values.length;pos++) { txtv.settext(values[position]); imgv.setimagebitmap(icon); } return v; } }
please help!
you should load contact , pass result adapter in background service e.g. asynctask, intentservice running long operations on ui thread crash application.
Comments
Post a Comment