expandablelistview - Navigational drawer doesn't open when not data network android -
i using navigational drawer showing expandablelistview
. every thing working perfect except 1 ... when no data network drawer doesn't opens ( when clicked on drawer icon nothing happens )
there no code explicitly open drawer when network available .
mainactivity.java
public class mainactivity extends activity implements onquerytextlistener { public int current_theme; /* google analytics*/ button btn_trackevent, btn_trackcrash; easytracker easytracker = null; googleanalytics googleanalytics; standardexceptionparser exceptionparser; /* end google analytics */ /* shared preference */ sharedpreferences sharedexplistview; sharedpreferences.editor editorexplist; /* end of shared preference */ private drawerlayout mdrawerlayout; //private listview explistview; expandablelistadapter listadapter; expandablelistview explistview; list<string> listdataheader; hashmap<string, list<string>> listdatachild,emptychildren; list<string> list_cat_name,list_cat_id,list_setting; private hashmap<string, arraylist<string>> emptychild; private actionbardrawertoggle mdrawertoggle; // nav drawer title private charsequence mdrawertitle; // used store app title private charsequence mtitle; // slide menu items private string[] navmenutitles; private typedarray navmenuicons; private textview mstatusview; private arraylist<navdraweritem> navdraweritems; private navdrawerlistadapter adapter; jsonarray cat = null; // hashmap listview // arraylist<hashmap<string, string>> catlist; int error_flag = 0; // 0 normal, 1 internet connvettion 2server error private textview main_error; private textview textview; string favcount = "0"; /* push */ // asyntask asynctask<void, void, void> mregistertask; public static string name = "all"; public static string email = "newsfirst@gmail.com"; @override protected void oncreate(bundle savedinstancestate) { // let's rid of app icon here actionbar actionbar = getactionbar(); actionbar.seticon(null); actionbar.settitle(""); super.oncreate(savedinstancestate); //overridependingtransition(r.anim.slide_in, r.anim.slide_out); //layoutinflater inflator = (layoutinflater) getsystemservice(context.layout_inflater_service); //view view = inflator.inflate(r.layout.action_bar_custom, null); setcontentview(r.layout.activity_main); // google analytics googleanalytics = googleanalytics.getinstance(this); googleanalytics.setdryrun(true); googleanalytics.getlogger().setloglevel(loglevel.verbose); easytracker = easytracker.getinstance(mainactivity.this); easytracker.set(fields.screen_name, "mainactivity"); easytracker.send(mapbuilder.createappview().build()); exceptionparser = new standardexceptionparser(mainactivity.this, null); // end google analytics /* polling webservice*/ strictmode.threadpolicy policy1 = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy1); /* shared preference */ sharedexplistview=this.getsharedpreferences("sharedexplistview", mode_private); editorexplist=sharedexplistview.edit(); // mtitle = mdrawertitle = gettitle(); // load slide menu items navmenutitles = getresources().getstringarray(r.array.nav_drawer_items); // nav drawer icons resources navmenuicons = getresources() .obtaintypedarray(r.array.nav_drawer_icons); mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); navdraweritems = new arraylist<navdraweritem>(); // home navdraweritems.add(new navdraweritem(navmenutitles[0], "", navmenuicons .getresourceid(0, -1), "0")); favcount = string.valueof(all_link.getfavrowcount(mainactivity.this)); // initialize expandable listview explistview = (expandablelistview) findviewbyid(r.id.list_slidermenu); explistview.setgroupindicator(null); list_cat_name = new arraylist<string>(); list_cat_id = new arraylist<string>(); list_setting = new arraylist<string>(); listdataheader = new arraylist<string>(); listdatachild = new hashmap<string, list<string>>(); emptychild = new hashmap<string, arraylist<string>>(); new getcatlist().execute(); mdrawertoggle = new actionbardrawertoggle(this, mdrawerlayout, r.drawable.ic_drawer, // nav menu toggle icon 0, // nav drawer open - description // accessibility 0 // nav drawer close - description // accessibility ) { @override public void ondrawerclosed(view view) { getactionbar().settitle(mtitle); // calling onprepareoptionsmenu() show action bar icons invalidateoptionsmenu(); } @override public void ondraweropened(view drawerview) { getactionbar().settitle(mdrawertitle); // calling onprepareoptionsmenu() hide action bar icons //toast.maketext(mainactivity.this, "network error", toast.length_short).show(); invalidateoptionsmenu(); } }; mdrawerlayout.setdrawerlistener(mdrawertoggle); if (savedinstancestate == null) { // on first time display view first nav item displayview(0, "0"); } } /** * slide menu item click listener * */ private class slidemenuclicklistener implements listview.onitemclicklistener { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { log.e("position", "karpo "+position); textview txt_id = (textview) view.findviewbyid(r.id.txt_id); string catid = txt_id.gettext().tostring(); textview txt_title = (textview) view.findviewbyid(r.id.title); all_link.global_title = ""; all_link.global_title = txt_title.gettext().tostring(); // display view selected nav drawer item if (!catid.isempty()) displayview(position, catid); } } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // toast.maketext(this, "selected item: " + item.gettitle(), // toast.length_short).show(); // toggle nav drawer on selecting action bar app icon/title if (mdrawertoggle.onoptionsitemselected(item)) { return true; } // handle action bar actions click switch (item.getitemid()) { /* * case r.id.action_search: * * return true; */ default: return super.onoptionsitemselected(item); } } /* * * called when invalidateoptionsmenu() triggered */ @override public boolean onprepareoptionsmenu(menu menu) { // if nav drawer opened, hide action items // boolean draweropen = mdrawerlayout.isdraweropen(explistview); // menu.finditem(r.id.action_search).setvisible(!draweropen); return super.onprepareoptionsmenu(menu); } /** * diplaying fragment view selected nav drawer list item * */ private void displayview(int position, string id) { // update main content replacing fragments fragment fragment = null; log.e("position", string.valueof(id)); int position_id = integer.parseint(id); switch (position_id) { case 0: // update selected item , title, close drawer try{ fragment = new homefragment(); easytracker.send(mapbuilder.createevent("menu click", "fragment", "homefragment", null).build()); } catch(exception e){ easytracker.send(mapbuilder.createexception(exceptionparser.getdescription(thread.currentthread().getname(), e), false).build()); } break; default: fragment = new categoryfragment(id); log.e("category id","karcat "+id); break; } // update selected item , title, close drawer // explistview.setitemchecked(position, true); // explistview.setselection(position); // //settitle(navmenutitles[position]); // mdrawerlayout.closedrawer(explistview); if (fragment != null) { fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction() .replace(r.id.frame_container, fragment).commit(); explistview.setitemchecked(position, true); explistview.setselection(position); // settitle(navmenutitles[position]); mdrawerlayout.closedrawer(explistview); } else { // error in creating fragment log.e("mainactivity", "error in creating fragment"); } } @override public void settitle(charsequence title) { mtitle = title; getactionbar().settitle(mtitle); } @override protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); // sync toggle state after onrestoreinstancestate has occurred. mdrawertoggle.syncstate(); } @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); // pass configuration change drawer toggls mdrawertoggle.onconfigurationchanged(newconfig); } /** * async task class json making http call * */ private class getcatlist extends asynctask<string, void, void> { @override protected void onpreexecute() { //explistview.setvisibility(8); super.onpreexecute(); // adding group data listdataheader.add("home"); listdataheader.add("news category"); listdataheader.add("my favourites"); listdataheader.add("manage notifications"); listdataheader.add("send story"); listdataheader.add("news on whatsapp"); listdataheader.add("about newsfirst"); listdataheader.add("contact newsfirst"); listdataheader.add("share app"); listdataheader.add("rate app"); listdataheader.add("settings"); listdatachild.put(listdataheader.get(0), new arraylist<string>()); // no child listdatachild.put(listdataheader.get(2), new arraylist<string>()); // no child listdatachild.put(listdataheader.get(3), new arraylist<string>()); // no child listdatachild.put(listdataheader.get(4), new arraylist<string>()); // no child listdatachild.put(listdataheader.get(5), new arraylist<string>()); // no child listdatachild.put(listdataheader.get(6), new arraylist<string>()); // no child listdatachild.put(listdataheader.get(7), new arraylist<string>()); // no child listdatachild.put(listdataheader.get(8), new arraylist<string>()); // no child listdatachild.put(listdataheader.get(9), new arraylist<string>()); // no child list_setting.add("faqs"); list_setting.add("privacy policy"); list_setting.add("terms & conditions"); listdatachild.put(listdataheader.get(10), list_setting); // child } @override protected void doinbackground(string... params) { if (networkcheck.isnetworkavailable(getapplicationcontext()) == true) { //navdraweritems = new arraylist<navdraweritem>(); // creating service handler class instance servicehandler sh = new servicehandler(); // making request url , getting response string jsonstr = sh.makeservicecall(all_link.category_url, servicehandler.get); log.d("response: ", "> " + jsonstr); if (jsonstr != null) { try { jsonobject jsonobj = new jsonobject(jsonstr); // getting json array node cat = jsonobj.getjsonarray(all_link.tag_news); string err = jsonobj.getstring(all_link.tag_error); log.e("------>error", string.valueof(err)); if (err.equals("1")) { } else { // looping through contacts (int = 0; < cat.length(); i++) { jsonobject c = cat.getjsonobject(i); string cat_id = c.getstring(all_link.tag_cat_id); string cat_name = c .getstring(all_link.tag_cat_name); string image_icon = c.getstring("image_icon"); list_cat_name.add(cat_name); list_cat_id.add(cat_id); } // saving cat_name cat id in shared preference (int = 0; <list_cat_name.size(); i++) { editorexplist.putstring(list_cat_name.get(i), list_cat_id.get(i)); editorexplist.commit(); } // end saving cat_name cat id in shared preference listdatachild.put(listdataheader.get(1), list_cat_name); // child } } catch (jsonexception e) { e.printstacktrace(); } } else { log.e("servicehandler", "couldn't data url"); runonuithread(new runnable() { @override public void run() { // todo auto-generated method stub toast.maketext(mainactivity.this, "network error", toast.length_short).show(); } }); } } else { log.e("network error", "internet connection error"); error_flag = 1; // error = "internet connection error"; } return null; } @override protected void onpostexecute(void result) { super.onpostexecute(result); if (error_flag == 1) { explistview.setvisibility(8); // main_error.settext("internet connection error! please check network settings , try again"); // main_error.setvisibility(0); } else { /* * navdraweritems.add(new navdraweritem(navmenutitles[1], * navmenuicons .getresourceid(0, -1),"1")); */ explistview.setvisibility(0); // main_error.settext(""); // main_error.setvisibility(8); // recycle typed array navmenuicons.recycle(); explistview .setonitemclicklistener(new slidemenuclicklistener()); // setting nav drawer list adapter /*adapter = new navdrawerlistadapter(getapplicationcontext(), navdraweritems);*/ listadapter=new expandablelistadapter(mainactivity.this, listdataheader, listdatachild); explistview.setadapter(listadapter); explistview.setitemchecked(0, true); explistview.setselection(0); } } } }
presumably, drawer consists of expandablelistview explistview
. if check asynctask's doinbackground()
method, you'll see error_flag
set 1
if there's no network available. then, in onpostexecute()
method:
if (error_flag == 1) { explistview.setvisibility(8);
a value of 8
corresponds view.gone
, so, essentially, if there no network, there no drawer.
Comments
Post a Comment