sqlite - Android: ListView Load gives Null Pointer Exception -
i'm trying data sqlite listview followed example in answer in url make listview sqlite database
the customcursoradapter going
public class customcursoradapter extends simplecursoradapter { private int layout; private layoutinflater inflater; private context context; public customcursoradapter (context context, int layout, cursor c, string[] from, int[] to) { super(context, layout, c, from, to); this.layout = layout; this.context = context; inflater = layoutinflater.from(context); } @override public view newview(context context, cursor cursor, viewgroup parent) { view v = inflater.inflate(r.layout.topics, parent, false); return v; } @override public void bindview(view v, context context, cursor c) { //1 column you're getting data string name = c.getstring(1); /** * next set name of entry. */ textview name_text = (textview) v.findviewbyid(r.id.listlabel); if (name_text != null) { name_text.settext(name); } } }
but oncreate have code
listview listview = (listview)findviewbyid(r.layout.topics); connecttodb(); = new string[] { "topictitle" }; = new int[] { r.id.listlabel }; customcursoradapter adapter = new customcursoradapter(this, r.layout.topics, cursor, from, to); listview.setadapter(adapter);
with xml follows
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" > <imageview android:id="@+id/listlogo" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginleft="5dp" android:layout_marginright="20dp" android:layout_margintop="5dp" > </imageview> <textview android:id="@+id/listlabel" android:layout_width="250dp" android:layout_height="wrap_content" android:text="@+id/label" android:textsize="15sp" > </textview> </linearlayout>
the problem .. listview null!! .. don't know why!!
you refering layout topics. should have listview defined in xml id. in activity oncreate() after setcontentview(r.layout.topics), fins id of listview , set adapter list.
in xml topics.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#0095ff"> <listview android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="100dp" android:id="@+id/lv"> </listview> // other ui elements </linearlayout>
then in activity oncreate()
setcontentview(r.layout.topics); listview listview = (listview)findviewbyid(r.id.lv); connecttodb(); = new string[] { "topictitle" }; = new int[] { r.id.listlabel }; customcursoradapter adapter = new customcursoradapter(this, r.layout.topics, cursor, from, to); listview.setadapter(adapter);
from discussion in chat
edit:
define main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#0095ff"> <listview android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="0dip" android:focusableintouchmode="false" android:listselector="@android:color/transparent" android:layout_weight="2" android:headerdividersenabled="false" android:footerdividersenabled="false" android:dividerheight="8dp" android:divider="#000000" android:cachecolorhint="#000000" android:drawselectorontop="false"> </listview> </linearlayout>
in activivity
public class mainactivity extends activity { listview lv; customcursoradapter cus; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); lv = (listview) findviewbyid(r.id.list); cus= new customcursoradapter(parameters); lv.setadapter(cus); } }
define customcursor adapter inflate custom layout. set image image view , text textview.
Comments
Post a Comment