android - How to extract data from mysql for listview in tab? -


i had created activity class , fragments each of listview. each tab display list of items according category in tab.

activity class:

public class displaypostitem extends fragmentactivity implements actionbar.tablistener {  private static final string state_selected_navigation_item = "selected_navigation_item";  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_display_post_item);      // set action bar.     final actionbar actionbar = getactionbar();     actionbar.setnavigationmode(actionbar.navigation_mode_tabs);      // each of sections in app, add tab action bar.     actionbar.addtab(actionbar.newtab().settext("books").settablistener(this));     actionbar.addtab(actionbar.newtab().settext("lab coat").settablistener(this));     actionbar.addtab(actionbar.newtab().settext("tools").settablistener(this));     actionbar.addtab(actionbar.newtab().settext("others").settablistener(this)); }  @override public void onrestoreinstancestate(bundle savedinstancestate) {     if (savedinstancestate.containskey(state_selected_navigation_item)) {         getactionbar().setselectednavigationitem(savedinstancestate.getint(state_selected_navigation_item));     } }  @override public void onsaveinstancestate(bundle outstate) {     outstate.putint(state_selected_navigation_item, getactionbar().getselectednavigationindex()); }  @override public boolean oncreateoptionsmenu(menu menu) {     getmenuinflater().inflate(r.menu.display_post_item, menu);     return true; }    @override public void ontabunselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { }  @override public void ontabselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) {      /**      * on first tab show our list      */     if (tab.getposition() == 0) {         booksfragment booklist = new booksfragment();         getsupportfragmentmanager().begintransaction().replace(r.id.container, booklist).commit();     }      else if (tab.getposition() == 1) {         coatfragment coatlist = new coatfragment();         getsupportfragmentmanager().begintransaction().replace(r.id.container, coatlist).commit();     }     else if (tab.getposition() == 2) {         toolfragment toollist = new toolfragment();         getsupportfragmentmanager().begintransaction().replace(r.id.container, toollist).commit();     }      else {          otherfragment otherlist = new otherfragment();         getsupportfragmentmanager().begintransaction().replace(r.id.container, otherlist).commit();     } }  @override public void ontabreselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { }  /**  * dummy fragment representing section of app, displays dummy text.  */ public static class dummysectionfragment extends fragment {     public dummysectionfragment() {     }      public static final string arg_section_number = "section_number";      @override     public view oncreateview(layoutinflater inflater, viewgroup container,             bundle savedinstancestate) {         textview textview = new textview(getactivity());         textview.setgravity(gravity.center);         bundle args = getarguments();         textview.settext(integer.tostring(args.getint(arg_section_number)));         return textview;     } } } 

--edit--

is right way information mysql? code below has several errors.

fragment class:

public class booksfragment extends listfragment {  // progress dialog private progressdialog pdialog;  // creating json parser object jsonparser jparser = new jsonparser(); jsonobject json;  arraylist<products> productslist = new arraylist<products>();  // url products list private static string url_all_products = "http://10.0.2.2:8000/project/display_items.php";  // json node names private static final string tag_success = "success"; private static final string tag_products = "product"; private static final string tag_pid = "pid"; private static final string tag_name = "name"; private static final string tag_budget = "price"; private static final string tag_des = "description"; private static final string tag_date_posted = "created_at"; private static final string tag_category = "category"; private static final string tag_email = "email"; private static final string tag_contact = "contact";  // products jsonarray jsonarray products = null;   @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);      listadapter listadapter = new arrayadapter<string>(getactivity(),             android.r.layout.simple_list_item_1, books);     setlistadapter(listadapter);      // hashmap listview     productslist = new arraylist<products>();      // loading products in background thread     new loadallproducts().execute(); }  @override public view oncreateview(layoutinflater inflater, viewgroup container,         bundle savedinstancestate) {     return inflater.inflate(r.layout.list_fragment, container, false);      listview lv = getlistview();      // on seleting single product     // launching edit product screen     lv.setonitemclicklistener(new onitemclicklistener() {          @override         public void onitemclick(adapterview<?> parent, view view,                 int position, long id) {             // getting values selected listitem             string pid = ((textview) view.findviewbyid(r.id.pid)).gettext()                     .tostring();             string name = ((textview) view.findviewbyid(r.id.name))                     .gettext().tostring();             string description = ((textview) view.findviewbyid(r.id.des))                     .gettext().tostring();             string price = ((textview) view.findviewbyid(r.id.budget))                     .gettext().tostring();             string date = ((textview) view.findviewbyid(r.id.date_posted))                     .gettext().tostring();             string category = ((textview) view.findviewbyid(r.id.category))                     .gettext().tostring();             string email = ((textview) view                     .findviewbyid(r.id.email_request)).gettext().tostring();             string contact = ((textview) view                     .findviewbyid(r.id.contact_request)).gettext()                     .tostring();              // starting new intent             intent in = new intent(getapplicationcontext(),                     displayiteminfo.class);             // sending information next activity             in.putextra(tag_pid, pid);             in.putextra(tag_name, name);             in.putextra(tag_des, description);             in.putextra(tag_budget, price);             in.putextra(tag_date_posted, date);             in.putextra(tag_category, category);             in.putextra(tag_email, email);             in.putextra(tag_contact, contact);             startactivity(in);         }     }); }  class loadallproducts extends asynctask<string, string, string> {      /**      * before starting background thread show progress dialog      * */     @override     protected void onpreexecute() {         super.onpreexecute();         pdialog = new progressdialog(booksfragment.this);         pdialog.setmessage("loading products. please wait...");         pdialog.setindeterminate(false);         pdialog.setcancelable(false);         pdialog.show();     }      /**      * getting products url      * */     protected string doinbackground(string... args) {         // building parameters         list<namevaluepair> params = new arraylist<namevaluepair>();         intent intent = getintent();         // getting attached intent data         string jsons = intent.getstringextra("json");          if (jsons == null) {             // getting json string url             json = jparser.makehttprequest(url_all_products, "get", params);         } else {             try {                 jsonobject jsonobj = new jsonobject(jsons);                 json = jsonobj;             } catch (jsonexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }         }          // check log cat json reponse         log.d("all products: ", json.tostring());          try {             // checking success tag             int success = json.getint(tag_success);              if (success == 1) {                 // products found                 // getting array of products                 products = json.getjsonarray(tag_products);                  // looping through products                 (int = 0; < products.length(); i++) {                     jsonobject c = products.getjsonobject(i);                      // storing each json item in variable                     string id = c.getstring(tag_pid);                     string name = c.getstring(tag_name);                     string budget = c.getstring(tag_budget);                     string description = c.getstring(tag_des);                     string category = c.getstring(tag_category);                     string contact = c.getstring(tag_contact);                     string email = c.getstring(tag_email);                     string date = c.getstring(tag_date_posted);                      if (category == "books") {                      // create new product obect, set values                     products mproduct = new products();                     mproduct.pid = id;                     mproduct.name = name;                     mproduct.price = budget;                     mproduct.description = description;                     mproduct.date = date;                     mproduct.category = category;                     mproduct.email = email;                     mproduct.contact = contact;                      // adding product arraylist                     productslist.add(mproduct);                     }                 }             } else {                 // no products found                 // launch add new product activity                 intent = new intent(getapplicationcontext(),                         mainmenu.class);                 // closing previous activities                 i.addflags(intent.flag_activity_clear_top);                 startactivity(i);             }         } catch (jsonexception e) {             e.printstacktrace();         }          return null;     }      /**      * after completing background task dismiss progress dialog      * **/      protected void onpostexecute(string file_url) {         // dismiss dialog after getting products         pdialog.dismiss();          if (productslist.size() > 0) {             /**              * updating parsed json data listview              * */             customadapter adapter = new customadapter(getbasecontext(),                     productslist);// ,              if (getlistview() != null) {                  // updating listview                 setlistadapter(adapter);             } else {                 log.d("listview-reference", "listview null");             }         } else {             log.d("product list", "products list empty");         }     } } } 

when select tab related of fragment class called have place populate listview code on oncreateview method because oncreate calling when fragment class set not called again , again oncreate view setup own fragment view called code in oncreateview method.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -