android - adding an intractable listview to fragment for BottomNavigationView -
i trying display intractable list view (among other things) inside fragment used bottonnavigationview. have done is, create main activity contains bottomnavigationview, , fragments contain other views. listview should connect firebase database. code files area follows:
joblist.java
package com.example.sherwin.todo; import android.content.intent; import android.os.bundle; import android.support.v4.app.fragment; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.button; import android.widget.listview; import com.google.firebase.database.childeventlistener; import com.google.firebase.database.datasnapshot; import com.google.firebase.database.databaseerror; import com.google.firebase.database.databasereference; import com.google.firebase.database.firebasedatabase; public class joblist extends fragment { public joblist() { } public static joblist newinstance() { joblist fragment = new joblist(); return fragment; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view rootview = inflater.inflate(r.layout.fragment_job_list, container, false); // create new adapter final arrayadapter<string> adapter = new arrayadapter<>(getactivity(), android.r.layout.simple_list_item_1, android.r.id.text1); // listview object xml final listview listview = (listview)rootview.findviewbyid(r.id.listview); // assign adapter listview listview.setadapter(adapter); // connect firebase database firebasedatabase database = firebasedatabase.getinstance(); // reference todoitems child items database final databasereference myref = database.getreference("jobs"); // assign listener detect changes child items // of database reference. myref.addchildeventlistener(new childeventlistener() { // function called once each child exists // when listener added. called // each time new child added. @override public void onchildadded(datasnapshot datasnapshot, string previouschildname) { string value = datasnapshot.getkey(); adapter.add(value); } // function called each time child item removed. public void onchildremoved(datasnapshot datasnapshot) { string value = datasnapshot.getkey(); adapter.remove(value); } // following functions required in childeventlistener implementations. public void onchildchanged(datasnapshot datasnapshot, string previouschildname) { } public void onchildmoved(datasnapshot datasnapshot, string previouschildname) { } @override public void oncancelled(databaseerror error) { // failed read value log.w("tag:", "failed read value.", error.toexception()); } }); ///to send next page button nextpage = (button) rootview.findviewbyid(r.id.addnewjob); // capture button clicks nextpage.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { // start newactivity.class intent myintent = new intent(getcontext(), jobform.class); startactivity(myintent); } }); return rootview; } }
mainactivity.java
package com.example.name.todo; import android.support.annotation.nonnull; import android.support.design.widget.bottomnavigationview; import android.support.v4.app.fragment; import android.support.v4.app.fragmenttransaction; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.menuitem; import android.widget.textview; public class mainactivity extends appcompatactivity { private textview mtextmessage; private bottomnavigationview.onnavigationitemselectedlistener monnavigationitemselectedlistener = new bottomnavigationview.onnavigationitemselectedlistener() { @override public boolean onnavigationitemselected(@nonnull menuitem item) { fragment selectedfragment = null; switch (item.getitemid()) { case r.id.navigation_home: selectedfragment = homepage.newinstance(); break; case r.id.navigation_job: selectedfragment = joblist.newinstance(); break; case r.id.navigation_machine: selectedfragment = machinepage.newinstance(); break; case r.id.navigation_mail: selectedfragment = messagepage.newinstance(); break; case r.id.navigation_resources: selectedfragment = resourcepage.newinstance(); break; } fragmenttransaction transaction = getsupportfragmentmanager().begintransaction(); transaction.replace(r.id.content, selectedfragment); transaction.commit(); return true; } }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); bottomnavigationview navigation = (bottomnavigationview) findviewbyid(r.id.navigation); navigation.setonnavigationitemselectedlistener(monnavigationitemselectedlistener); } }
fragment_job_list.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout 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:orientation="vertical"> <relativelayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2.8"> <listview android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_above="@+id/addnewjob" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_alignparenttop="true"></listview> <button android:id="@+id/addnewjob" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:text="@string/add_job" /> </relativelayout> </linearlayout>
the logcat spits out following:
java.lang.runtimeexception: unable instantiate activity componentinfo{com.example.name.todo/com.example.name.todo.joblist}: java.lang.classcastexception: com.example.name.todo.joblist cannot cast android.app.activity @ android.app.activitythread.performlaunchactivity(activitythread.java:2567) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2726) @ android.app.activitythread.-wrap12(activitythread.java) @ android.app.activitythread$h.handlemessage(activitythread.java:1477) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:154) @ android.app.activitythread.main(activitythread.java:6119) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:886) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:776) caused by: java.lang.classcastexception: com.example.name.todo.joblist cannot cast android.app.activity @ android.app.instrumentation.newactivity(instrumentation.java:1078) @ android.app.activitythread.performlaunchactivity(activitythread.java:2557) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2726) @ android.app.activitythread.-wrap12(activitythread.java) @ android.app.activitythread$h.handlemessage(activitythread.java:1477) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:154) @ android.app.activitythread.main(activitythread.java:6119) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:886) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:776)
i not sure here appreciated.
thanks
what @johnc said. updated manifest reflect joblist.java not activity
Comments
Post a Comment