android - How to use onitemClickListener with Fragments? -
i trying implement onitemclicklistener in fragment class unfortunately not working properly... here source code.. please let me know error??
networkdetailsfragment.java(my fragment class)
public class networkdetailsfragment extends fragment implements adapterview.onitemclicklistener{
private listview listview; private view networkdetailsview; private qosnetworkdetailsadapter qosnetworkdetailsadapter; private qosnetworkdetailsdatabasehelper qosnetworkdetailsdatabasehelper; private simplecursoradapter simplecursoradapter; private string log_tag = networkdetailsfragment.class.getsimplename(); @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { log.i(log_tag, "inside oncreateview() method"); networkdetailsview = inflater.inflate(r.layout.network_details, container, false); listview = (listview) networkdetailsview.findviewbyid(r.id.list_view); qosnetworkdetailsdatabasehelper = new qosnetworkdetailsdatabasehelper(getactivity()); cursor cursor = qosnetworkdetailsdatabasehelper.getrecord(); string[] columns = {qosnetworkdetailsdatabaseconstants.column_network_type, qosnetworkdetailsdatabaseconstants.column_network_status, qosnetworkdetailsdatabaseconstants.column_latitude, qosnetworkdetailsdatabaseconstants.column_longitude, qosnetworkdetailsdatabaseconstants.column_time}; int[] = new int[]{ r.id.networktype, r.id.networkstatus, r.id.latitudeandlongitude, r.id.date }; /*simplecursoradapter simplecursoradapter = new simplecursoradapter( this, r.layout.activity_network_info, cursor, columns, to, 0);*/ simplecursoradapter = new simplecursoradapter(getactivity(), r.layout.activity_network_info, cursor, columns, to, 0); qosnetworkdetailsadapter = new qosnetworkdetailsadapter(getactivity().getapplicationcontext(), qosnetworkdetailsdatabasehelper.getrecord(), false); listview.setadapter(qosnetworkdetailsadapter); return networkdetailsview; } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); listview.setonitemclicklistener(this); } @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { cursor cursor = (cursor) parent.getitematposition(position); string countrycode = cursor.getstring(cursor.getcolumnindexorthrow("code")); toast.maketext(getactivity().getapplicationcontext(), countrycode, toast.length_short).show(); } }
network_details.xml(i.e list_view.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="match_parent" android:contentdescription="@string/network_details_fragment_string" android:descendantfocusability="blocksdescendants" > <listview android:id="@+id/list_view" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </linearlayout> activity_network_info.xml(list items) <scrollview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scroller" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fillviewport="true" android:descendantfocusability="blocksdescendants"> <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="@drawable/black" > <textview android:id="@+id/networkstatus" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textcolor="@drawable/white" android:textsize="15sp" /> <textview android:id="@+id/networktype" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textcolor="@drawable/white" android:textsize="15sp" /> <textview android:id="@+id/latitudeandlongitude" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textcolor="@drawable/lightblue" android:textsize="15sp" /> <textview android:id="@+id/date" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textcolor="@drawable/lightblue" android:textsize="15sp" /> </linearlayout> </scrollview>
list getting populated screen... onitemclicklistener not working in code... please let me know error.. thanks
try extend fragmentactivity instead of fragment...
Comments
Post a Comment