android - Dealing with Fragment and listView and JSON -
i trying make application menudrawer contains 4 fragments. how application show do. when starts, should retrieve data website. retrieved data should in listview in first fragment. second fragment should contain webview view page in android device. third show text details. application crashes when first fragment gets details website. here code : fragment layout following:
my first fragment contains following:
<listview android:layout_margintop="10dp" android:layout_below="@+id/txtlabel" android:layout_width="match_parent" android:layout_height="300dp" android:background = "@android:color/transparent" android:padding="3dp" android:dividerheight="1dp" android:id="@+id/mlv"></listview>
the first fragment called when app starts. have list adapter , list model worked well.
my main class contains inner class gets data. made model class , adapter class: here main code :
public class mainactivity extends activity { // declare properties private string[] mnavigationdraweritemtitles; private drawerlayout mdrawerlayout; private listview mdrawerlist; private listview mlistview; private listviewnewsadapter listviewnewsadapter; private arraylist<listviewnewsitem> listviewnewsitems; private jsonparser jsonparser = new jsonparser(); private string readnews_url = "xyz-myweb-xyz.com"; private actionbardrawertoggle mdrawertoggle; // nav drawer title private charsequence mdrawertitle; // used store app title private charsequence mtitle; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mlistview = (listview) findviewbyid(r.id.mlv); // proper titles mtitle = mdrawertitle = gettitle(); // initialize properties mnavigationdraweritemtitles = getresources().getstringarray(r.array.navigation_drawer_items_array); mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); mdrawerlist = (listview) findviewbyid(r.id.left_drawer); // list drawer items objectdraweritem[] draweritem = new objectdraweritem[4]; draweritem[0] = new objectdraweritem(r.drawable.home_icon, "home"); draweritem[1] = new objectdraweritem(r.drawable.website_icon, "website"); draweritem[2] = new objectdraweritem(r.drawable.ic_action_share, "help"); // pass folderdata our listview adapter draweritemcustomadapter adapter = new draweritemcustomadapter(this, r.layout.listview_item_row, draweritem); // set adapter list view mdrawerlist.setadapter(adapter); // set item click listener mdrawerlist.setonitemclicklistener(new draweritemclicklistener()); // app icon control nav drawer mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); mdrawertoggle = new actionbardrawertoggle( this, /* host activity */ mdrawerlayout, /* drawerlayout object */ r.drawable.ic_drawer, /* nav drawer icon replace 'up' caret */ r.string.drawer_open, /* "open drawer" description */ r.string.drawer_close /* "close drawer" description */ ) { /** called when drawer has settled in closed state. */ public void ondrawerclosed(view view) { super.ondrawerclosed(view); getactionbar().settitle(mtitle); } /** called when drawer has settled in open state. */ public void ondraweropened(view drawerview) { super.ondraweropened(drawerview); getactionbar().settitle(mdrawertitle); } }; // set drawer toggle drawerlistener mdrawerlayout.setdrawerlistener(mdrawertoggle); // enable actionbar app icon behave action toggle nav drawer getactionbar().setdisplayhomeasupenabled(true); getactionbar().sethomebuttonenabled(true); if (savedinstancestate == null) { // on first time display view first nav item selectitem(0); } }
the method deals fragments, it's inside mymainactivity :
private void selectitem(int position) { // update main content replacing fragments fragment fragment = null; switch (position) { case 0: fragment = new createfragment(); new getnewstask().execute(); break; case 1: fragment = new readfragment(); break; case 2: fragment = new helpfragment(); break; default: break; } if (fragment != null) { fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction().replace(r.id.content_frame, fragment).commit(); // update selected item , title, close drawer mdrawerlist.setitemchecked(position, true); mdrawerlist.setselection(position); settitle(mnavigationdraweritemtitles[position]); mdrawerlayout.closedrawer(mdrawerlist); } else { // error in creating fragment log.e("mainact10ity", "error in creating fragment"); } }
i used inner class retrieve data web:
private class getnewstask extends asynctask<void, void, boolean> { private progressdialog mprogressdialog; private jsonobject jsonobjectresult = null; private string error; @override protected void onpreexecute() { super.onpreexecute(); listviewnewsitems = new arraylist<listviewnewsitem>(); mprogressdialog = progressdialog.show(mainactivity.this, "processing...", "get last news", false, false); } @override protected boolean doinbackground(void... params) { jsonobjectresult = jsonparser.makehttprequest(readnews_url, null); if (jsonobjectresult == null) { error = "error in connection"; return false; } try { if (jsonobjectresult.getint("success") == 1) { jsonarray jsonarray = jsonobjectresult.getjsonarray("posts"); (int = 0; < jsonarray.length(); i++) { jsonobject news = jsonarray.getjsonobject(i); listviewnewsitem listviewnewsitem = new listviewnewsitem ( news.getstring("title"), news.getstring("type") ); listviewnewsitems.add(listviewnewsitem); } return true; } else error = jsonobjectresult.getstring("message"); } catch (exception ex) { } return false; } @override protected void onpostexecute(boolean aboolean) { super.onpostexecute(aboolean); mprogressdialog.dismiss(); if (aboolean) { listviewnewsadapter = new listviewnewsadapter(getapplicationcontext(), listviewnewsitems); mlistview.setadapter(listviewnewsadapter); } else toast.maketext(getapplicationcontext(), error, toast.length_long).show(); } }
where have use
new getnewstask().execute();
i tried use in oncreate, app crashed. tried use inside switch app crashes. have idea how fix ?
i fixed following: moving followings fragment :
private listview mlistview; private listviewnewsadapter listviewnewsadapter; private arraylist<listviewnewsitem> listviewnewsitems; private jsonparser jsonparser = new jsonparser(); private string readnews_url = "xxxxxxxx.xxxxx.xxxxx"; public createfragment() { // required empty public constructor }
then, inside oncreateview created :
mlistview = (listview) rootview.findviewbyid(r.id.mlv);
new getnewstask().execute();
after that, wrote inner class fragment. well, had change arguments make code work well. then, run , it's working property now. thank
Comments
Post a Comment