android - ListFragment with Navigation Drawer -
bit of noob @ android, made far. trying listview navigation drawer... following doesn't work unless uncomment setcontentview line. throws null ptr exception. when uncomment it, listview works, nav drawer quits showing up. also, onlistitemclick doesn't trigger, that's later problem me learn.
import java.util.arraylist; import java.util.list; import android.app.activity; import android.os.bundle; import android.support.v4.app.listfragment; import android.util.log; import android.view.view; import android.widget.listview; import android.widget.toast; public class all_listview extends listfragment { @override public void onlistitemclick(listview l, view v, int position, long id) { super.onlistitemclick(l, v, position, id); log.d("onclick", string.valueof(position)); activity = getactivity(); string item = (string) getlistadapter().getitem(position); toast.maketext(a, item + " selected", toast.length_long).show(); } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); arraylist<weekitem> wklist = new arraylist<>(); activity = getactivity(); // a.setcontentview(r.layout.all_listview); weekadapter adapter = new weekadapter(a.getbasecontext(), wklist); listview lv = (listview)a.findviewbyid(r.id.all_list_view); lv.setadapter(adapter); sqlitehelper db = new sqlitehelper(getactivity()); list<week> weeks = db.getall(); arraylist<weekitem> wkitem = weekitem.fromlist(weeks); adapter.addall(wkitem); } }
i'm sure there's more problem can see @ moment, welcomed.
mainactivity
package com.hourtracker; import android.support.v7.app.actionbaractivity; import android.support.v7.app.actionbar; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.content.pm.activityinfo; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem; import android.widget.toast; import android.support.v4.widget.drawerlayout; public class mainactivity extends actionbaractivity implements navigationdrawerfragment.navigationdrawercallbacks { /** * fragment managing behaviors, interactions , presentation of * navigation drawer. */ private navigationdrawerfragment mnavigationdrawerfragment; /** * used store last screen title. use in * {@link #restoreactionbar()}. */ private charsequence mtitle; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); log.e("hours","creating session filter"); setcontentview(r.layout.activity_main); setrequestedorientation(activityinfo.screen_orientation_portrait); mnavigationdrawerfragment = (navigationdrawerfragment) getsupportfragmentmanager() .findfragmentbyid(r.id.navigation_drawer); mtitle = gettitle(); // set drawer. mnavigationdrawerfragment.setup(r.id.navigation_drawer, (drawerlayout) findviewbyid(r.id.drawer_layout)); } @override public void onnavigationdraweritemselected(int position) { // update main content replacing fragments log.d("onnavdrawerselected", string.valueof(position)); fragmentmanager fragmentmanager = getsupportfragmentmanager(); switch(position) { case 0: fragmentmanager .begintransaction() .replace(r.id.frame_container, ((fragment)new entry_fragment())).commit(); break; case 1: fragmentmanager .begintransaction() .replace(r.id.frame_container, ((fragment)new week_fragment())).commit(); break; case 2: fragmentmanager .begintransaction() .replace(r.id.frame_container, ((fragment)new all_listview())).commit(); break; } } public void onsectionattached(int number) { switch (number) { case 1: mtitle = getstring(r.string.title_section1); break; case 2: mtitle = getstring(r.string.title_section2); break; case 3: mtitle = getstring(r.string.title_section3); break; } } public void restoreactionbar() { actionbar actionbar = getsupportactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_standard); actionbar.setdisplayshowtitleenabled(true); actionbar.settitle(mtitle); } @override public boolean oncreateoptionsmenu(menu menu) { if (!mnavigationdrawerfragment.isdraweropen()) { // show items in action bar relevant screen // if drawer not showing. otherwise, let drawer // decide show in action bar. getmenuinflater().inflate(r.menu.main, menu); restoreactionbar(); return true; } return super.oncreateoptionsmenu(menu); } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmentmanager .begintransaction() .replace(r.id.frame_container, ((fragment)new settings_fragment())).commit(); return true; } toast.maketext(this, "not settings", toast.length_short).show();; return super.onoptionsitemselected(item); }
}
activity_main.xml
<!-- main content view, view below consumes entire space available using match_parent in both dimensions. --> <framelayout android:id="@+id/frame_container" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- android:layout_gravity="start" tells drawerlayout treat sliding drawer on left side left-to-right languages , on right side right-to-left languages. if you're not building against api 17 or higher, use android:layout_gravity="left" instead. --> <!-- drawer given fixed width in dp , extends full height of container. --> <fragment android:id="@+id/navigation_drawer" android:name="com.hourtracker.navigationdrawerfragment" android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent" android:layout_gravity="start" /> </android.support.v4.widget.drawerlayout>
all_listview.xml
<?xml version="1.0" encoding="utf-8"?> <listview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/all_list_view" android:layout_width="match_parent" android:layout_height="match_parent" > </listview>
i think main error when create all_listview extends listfragment, suggest follow example helps create simple listfragment
after right think list fragment work , navigation drawer too, thats because when setcontentview listfragment wrong
Comments
Post a Comment