android - Listview inside fragment onitemclick is not working -
in application in main view there 3 tabs. 3 tabs filled seperate fragment viewpage adapter. in second tab , there listview (includes in fragment). when user clicks on listview need show fragment there. when user clicks on listview item got error this
no view found id 0x7f0e008a (com.example.abc.myapp:id/new_output) fragment mantrafragment{30391e5 #3 id=0x7f0e008a}
i found error in other questions in stackoverflow. tried solutions of them , didn't work. please me fix issue.
class includes listview
public class twofragment extends fragment { private listview listview; private string items[]; public twofragment() { } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); items = getresources().getstringarray(r.array.races_array); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_two, container, false); listview = (listview) rootview.findviewbyid(r.id.mylistview); customlistadapter adapter = new customlistadapter(getactivity(), items); listview.setadapter(adapter); listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { fragmentmanager fm = getactivity().getsupportfragmentmanager(); android.support.v4.app.fragmenttransaction ft = fm.begintransaction(); fragment newfrag = new mynewfragment(); bundle arguments = new bundle(); newfrag.setarguments(arguments); ft.replace(r.id.new_output, newfrag); ft.addtobackstack(null); ft.commit(); } }); return rootview; }}
mynewfragment.class
public class mynewfragment extends fragment{ public mynewfragment(){ } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview= inflater.inflate(r.layout.mynewfrag_view, container, false); return rootview; }}
mynewfragment view (mynewfrag_view)
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:gravity="center" android:layout_height="match_parent"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textcolor="#ff0000" android:textsize="20sp" android:id="@+id/msg2"/> <fragment android:layout_width="match_parent" android:layout_height="match_parent" android:background="#b0b0ff" android:id="@+id/new_output" /> </linearlayout>
mainactivity class
public class mainactivity extends appcompatactivity { private toolbar toolbar; private tablayout tablayout; private viewpager viewpager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); viewpager = (viewpager) findviewbyid(r.id.viewpager); setupviewpager(viewpager); tablayout = (tablayout) findviewbyid(r.id.tabs); tablayout.setupwithviewpager(viewpager); } private void setupviewpager(viewpager viewpager) { viewpageradapter adapter = new viewpageradapter(getsupportfragmentmanager()); adapter.addfragment(new onefragment(), "one"); adapter.addfragment(new twofragment(), "two"); adapter.addfragment(new threefragment(), "three"); viewpager.setadapter(adapter); } public class viewpageradapter extends fragmentpageradapter { private final list<fragment> mfragmentlist = new arraylist<>(); private final list<string> mfragmenttitlelist = new arraylist<>(); public viewpageradapter(fragmentmanager manager) { super(manager); } @override public fragment getitem(int position) { return mfragmentlist.get(position); } @override public int getcount() { return mfragmentlist.size(); } public void addfragment(fragment fragment, string title) { mfragmentlist.add(fragment); mfragmenttitlelist.add(title); } @override public charsequence getpagetitle(int position) { return mfragmenttitlelist.get(position); } }}
mainactivity layout(activity_main)
<android.support.design.widget.coordinatorlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.appbarlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/themeoverlay.appcompat.dark.actionbar"> <android.support.v7.widget.toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionbarsize" android:background="?attr/colorprimary" app:layout_scrollflags="scroll|enteralways" app:popuptheme="@style/themeoverlay.appcompat.light" /> <android.support.design.widget.tablayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabmode="fixed" app:tabgravity="fill"/> </android.support.design.widget.appbarlayout> <android.support.v4.view.viewpager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" />
ps : i’m still beginner in android development , think it’s use fragment there insted of showing activity. (correct me if i’m wrong)
you adding mynewfragment
, giving id
of view present in my_newfragment.xml
still unknown. have add/replace fragment in layout file of twofragment
i.e. fragment_two.xml
.
your fragment_two.xml
should like
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:gravity="center" android:layout_height="match_parent"> .... <fragment android:layout_width="match_parent" android:layout_height="match_parent" android:background="#b0b0ff" android:id="@+id/new_output" /> .... </linearlayout>
remove fragment
tag new_output
mynewfrag_view.xml
, add in fragment_two.xml
above, , check if works.
Comments
Post a Comment