android - Contacts in listview -
i making app in need read contacts , send text message.i able read contacts unable show contacts name , no in list view.
when try set c_number app crashes.
and want send sms selected contacts.
any cater problems thanks.
this single_listview_item.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <scrollview android:layout_width="fill_parent" android:layout_height="fill_parent"> <linearlayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <checkbox android:id="@+id/chk_box" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true"/> <textview android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_torightof="@id/chk_box" android:textstyle="bold"/> <textview android:id="@+id/number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/name" android:layout_torightof="@id/chk_box" android:textstyle="italic" android:textsize="12sp"/> </linearlayout> </scrollview> </relativelayout>
this main activity read contacts , trying set them list view . have searched lot unable find correct solution. me going .
this mainactivity.java
package chypher.listviewexample; import android.content.contentresolver; import android.database.cursor; import android.net.uri; import android.provider.contactscontract; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.button; import android.widget.compoundbutton; import android.widget.listview; import android.widget.simplecursoradapter; import android.widget.switch; import android.widget.toast; import java.util.arraylist; import java.util.list; public class mainactivity extends actionbaractivity implements compoundbutton.oncheckedchangelistener { listview lv; arraylist<planet> planetlist; contactadapter cadapter; button okbtn; public string name; public string phonenumber; public list<string> numb= new arraylist<string>();; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); lv=(listview)findviewbyid(r.id.listview); fetchcontacts(); //fetchcontacts(); } private void fetchcontacts() { // int phonenumber ; int i=0; uri content_uri = contactscontract.contacts.content_uri; string _id = contactscontract.contacts._id; string display_name = contactscontract.contacts.display_name; string has_phone_number = contactscontract.contacts.has_phone_number; uri phonecontent_uri = contactscontract.commondatakinds.phone.content_uri; string phone_contact_id = contactscontract.commondatakinds.phone.contact_id; string number = contactscontract.commondatakinds.phone.number; list<string>output = new arraylist<string>(); list<string>outputnum=new arraylist<string>(); contentresolver contentresolver = getcontentresolver(); cursor cursor = contentresolver.query(content_uri, null,null, null, null); // loop every contact in phone if (cursor.getcount() > 0) { while (cursor.movetonext()) { string contact_id = cursor.getstring(cursor.getcolumnindex( _id )); name = cursor.getstring(cursor.getcolumnindex( display_name )); // system.out.println("checking phonename variable"+fromphonename); int hasphonenumber = integer.parseint(cursor.getstring(cursor.getcolumnindex( has_phone_number ))); if (hasphonenumber > 0) { output.add(name); // query , loop every phone number of contact cursor phonecursor = contentresolver.query(phonecontent_uri, null, phone_contact_id + " = ?", new string[] { contact_id }, null); while (phonecursor.movetonext()) { phonenumber = phonecursor.getstring(phonecursor.getcolumnindex(number)); outputnum.add( phonenumber); // system.out.println("cursor phoneno:"+phonenumber); system.out.println("i value:"+i); i++; displayplanetlist(name,phonenumber); } phonecursor.close(); } } system.out.println("read phone:"+output.size()); system.out.println("read num:"+outputnum.size()); } } private void displayplanetlist(string nam,string nm) { planetlist =new arraylist<planet>(); planetlist.add(new planet(nam,nm)); cadapter=new contactadapter(planetlist,this); lv.setadapter(cadapter); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.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. switch (item.getitemid()) { case r.id.action_next: system.out.println("selected :" + numb); break; } return true; } @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { int pos=lv.getpositionforview(buttonview); if(pos!=listview.invalid_position){ planet p=planetlist.get(pos); p.setselected(ischecked); toast.maketext(this,"clicked on planet:" +p.getname()+" : state :"+ischecked,toast.length_short).show(); numb.add(p.getnumber()); } } }
this contact adapter class .
package chypher.listviewexample; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.checkbox; import android.widget.textview; import java.util.list; /** * created saqlain haider on 6/13/2015. */ class planet{ string name; string number; boolean selected=false; public planet(string name,string number){ super(); this.name=name; this.number=number; } public boolean isselected() { return selected; } public void setselected(boolean selected) { this.selected = selected; } public string getnumber() { return number; } public void setnumber(string number) { this.number = number; } public string getname() { return name; } public void setname(string name) { this.name = name; } } public class contactadapter extends arrayadapter<planet> { private list<planet>planetlist; private context context; public contactadapter(list<planet> planetlist, context context) { super(context, r.layout.single_listview_item,planetlist); this.planetlist=planetlist; this.context=context; } private static class planetholder{ public textview cname,cnumber; public checkbox checkbox; } @override public view getview(int position,view convertview,viewgroup parent){ view v=convertview; planetholder holder=new planetholder(); if(convertview==null){ layoutinflater inflater=(layoutinflater)context.getsystemservice(context.layout_inflater_service); v=inflater.inflate(r.layout.single_listview_item,null); holder.cname=(textview)v.findviewbyid(r.id.name); holder.cnumber=(textview)v.findviewbyid(r.id.number); holder.checkbox=(checkbox)v.findviewbyid(r.id.chk_box); holder.checkbox.setoncheckedchangelistener((mainactivity)context); } else { holder=(planetholder)v.gettag(); } planet p=planetlist.get(position); holder.cname.settext(p.getname()); holder.cnumber.settext(p.getnumber()); holder.checkbox.setchecked(p.isselected()); holder.checkbox.settag(p); return v; } }
this simple main.xml containing listview
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <listview android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent"></listview> </linearlayout>
error log:
06-14 00:01:59.972 10362-10362/chypher.listviewexample e/androidruntime﹕ fatal exception: main java.lang.nullpointerexception @ chypher.listviewexample.contactadapter.getview(contactadapter.java:82) @ android.widget.abslistview.obtainview(abslistview.java:2063) @ android.widget.listview.makeandaddview(listview.java:1792) @ android.widget.listview.filldown(listview.java:676) @ android.widget.listview.fillgap(listview.java:640) @ android.widget.abslistview.trackmotionscroll(abslistview.java:4901) @ android.widget.abslistview.scrollifneeded(abslistview.java:2923) @ android.widget.abslistview.startscrollifneeded(abslistview.java:2868) @ android.widget.abslistview.onintercepttouchevent(abslistview.java:3738) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1639) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2019) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1754) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2019) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1754) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2019) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1754) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2019) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1754) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2019) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1754) @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2019) @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1754) @ com.android.internal.policy.impl.phonewindow$decorview.superdispatchtouchevent(phonewindow.java:1928) @ com.android.internal.policy.impl.phonewindow.superdispatchtouchevent(phonewindow.java:1387) @ android.app.activity.dispatchtouchevent(activity.java:2388) @ android.support.v7.internal.view.windowcallbackwrapper.dispatchtouchevent(windowcallbackwrapper.java:59) @ com.android.internal.policy.impl.phonewindow$decorview.dispatchtouchevent(phonewindow.java:1876) @ android.view.view.dispatchpointerevent(view.java:5733) @ android.view.viewrootimpl.deliverpointerevent(viewrootimpl.java:3104) @ android.view.viewrootimpl.handlemessage(viewrootimpl.java:2666) @ android.view.viewrootimpl.processinputevents(viewrootimpl.java:900) @ android.view.viewrootimpl.handlemessage(viewrootimpl.java:2675) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:4666) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:809) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:576) @ dalvik.system.nativestart.main(native method)
updated error log:
06-14 01:24:17.271 14698-14698/chypher.listviewexample w/dalvikvm﹕ threadid=1: thread exiting uncaught exception (group=0x40e4c258) 06-14 01:24:17.305 14698-14698/chypher.listviewexample e/androidruntime﹕ fatal exception: main java.lang.nullpointerexception @ android.widget.adapterview.getpositionforview(adapterview.java:594) @ chypher.listviewexample.mainactivity.oncheckedchanged(mainactivity.java:153) @ android.widget.compoundbutton.setchecked(compoundbutton.java:125) @ chypher.listviewexample.contactadapter.getview(contactadapter.java:84) @ android.widget.abslistview.obtainview(abslistview.java:2063) @ android.widget.listview.makeandaddview(listview.java:1792) @ android.widget.listview.fillup(listview.java:709) @ android.widget.listview.fillgap(listview.java:649) @ android.widget.abslistview.trackmotionscroll(abslistview.java:4901) @ android.widget.abslistview$flingrunnable.run(abslistview.java:4074) @ android.os.handler.handlecallback(handler.java:605) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:4666) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:809) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:576) @ dalvik.system.nativestart.main(native method)
the problem is, you're never setting planetholder
(which should written in camel-case) tag of item-view.
this okay first items, since they'll need inflated anyways. start scrolling , inflated views re-used, run block:
} else { holder=(planetholder)v.gettag(); }
since tag never set on v
, v.gettag()
return null
. in java, can cast null
(see this discussion), causes holder
null
.
now, when call holder.cname
, you're trying access property on null
-reference, causes nullpointerexception
.
the fix change this:
holder.checkbox.settag(p);
to this:
v.settag(holder);
Comments
Post a Comment