Using customlistviews in android : Keep on getting list view = NULL -
i new android , trying create 2 fragments , have can slide between two, 1 list , otehr grid. had list working when when using arrayadapter , had eventlistfragment extending listfragment (if code helpful let me know , ill post part)
i trying create custom list view multiline list items (let me know if there easier way , if have overcomplicated whole thing)
here code:
event list fragment:
public class eventlistfragment extends fragment { arraylist<eventobject> eventobjects; @override public view oncreateview (layoutinflater inflater, viewgroup container, bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); eventobjects = ((eventsactivity)getactivity()).geteventobjects(); view view = inflater.inflate(r.layout.eventgrid ,container,false); listview listview = (listview) view.findviewbyid(r.id.listview); if (listview == null) { system.out.println("asas"); } listview.setadapter(new mycustombaseadapter(getactivity().getbasecontext(), eventobjects)); listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> a, view v, int position, long id) { object o = listview.getitematposition(position); eventobject fullobject = (eventobject)o; system.out.println("asd"); } }); return view; } }
the corresponding xml:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.view.viewpager android:id="@+id/viewpager" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <listview android:id="@+id/listview" android:layout_height="wrap_content" android:layout_width="fill_parent"/> </relativelayout>
the xml customgridrow:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <textview android:id="@+id/name" android:textsize="14sp" android:textstyle="bold" android:textcolor="#ffff00" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <textview android:id="@+id/citystate" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <textview android:id="@+id/phone" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </linearlayout>
edit
the getview()
mycustombaseadapter
:
public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { convertview = minflater.inflate(r.layout.customgridrow, null); holder = new viewholder(); holder.txtname = (textview) convertview.findviewbyid(r.id.name); holder.txtcitystate = (textview) convertview.findviewbyid(r.id.citystate); holder.txtphone = (textview) convertview.findviewbyid(r.id.phone); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.txtname.settext(events.get(position).getname()); holder.txtcitystate.settext(events.get(position).getdate()); holder.txtphone.settext(events.get(position).getvenue()); return convertview; }
from comments
you inflating wrong layout
you should inflate 1 has listview
, initialize same.
change
view view = inflater.inflate(r.layout.eventgrid ,container,false);
to
view view = inflater.inflate(r.layout.eventlist ,container,false);
Comments
Post a Comment