php - display data from mysql using json to listview in fragment -
i trying load data database using json , display in listview in fragment. have 2 classes, 1 called downloader , 1 called perser. add data listview in fragmnet.
how can achieve because work fine in content_mani want fragment
import android.app.progressdialog; import android.content.context; import android.os.asynctask; import android.widget.listview; import android.widget.toast; import java.io.bufferedinputstream; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import info.androidhive.materialtabs.fragments.onefragment; /** * created george on 25/06/2016. */ public class downloader extends asynctask<void ,integer,string>{ context c; string urladdress; listview lv; progressdialog progressdialog; public downloader(mainactivity context, listview listview, string urladdress) { this.c = context; this.urladdress = urladdress; this.lv = listview; } // before job start @override protected void onpreexecute() { super.onpreexecute(); progressdialog=new progressdialog(c); progressdialog.settitle("fetch data"); progressdialog.setmessage("fetching data.....please wait"); progressdialog.show(); } @override protected string doinbackground(void... params) { string data=downloaddata(); return data; } @override protected void onprogressupdate(integer... values) { super.onprogressupdate(values); } @override protected void onpostexecute(string s) { super.onpostexecute(s); progressdialog.dismiss(); if(s!=null){ parser p=new parser(c,s,lv); p.execute(); }else { toast.maketext(c,"unable download data",toast.length_long).show(); } } private string downloaddata(){ inputstream inputstream=null; string line=null; try{ url url=new url(urladdress); httpurlconnection httpurlconnection=(httpurlconnection) url.openconnection(); inputstream=new bufferedinputstream(httpurlconnection.getinputstream()); bufferedreader bufferedreader=new bufferedreader(new inputstreamreader(inputstream)); stringbuffer stringbuffer=new stringbuffer(); if (bufferedreader!=null) { while ((line=bufferedreader.readline())!=null) { stringbuffer.append(line+"\n"); } }else { return null; } return stringbuffer.tostring(); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } { if (inputstream!=null) { try { inputstream.close(); } catch (ioexception e) { e.printstacktrace(); } } } return null; } } , package info.androidhive.materialtabs.activity; import android.app.*; import android.content.context; import android.os.asynctask; import android.widget.*; import info.androidhive.materialtabs.r; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import java.util.*; public class parser extends asynctask<void,integer,integer> { context c; listview lv; string data; arraylist<string> users=new arraylist<>(); progressdialog pd; public parser(context c, string data, listview lv) { this.c = c; this.data = data; this.lv = lv; } @override protected void onpreexecute() { super.onpreexecute(); pd=new progressdialog(c); pd.settitle("parser"); pd.setmessage("parsing....please wait"); pd.show(); } @override protected integer doinbackground(void... params) { return this.perse(); } @override protected void onpostexecute(integer integer) { super.onpostexecute(integer); if(integer==1){ arrayadapter<string> adapter=new arrayadapter<string>(c, android.r.layout.simple_list_item_1,users); lv.setadapter(adapter); } else { toast.maketext(c,"unable perse",toast.length_long).show(); } pd.dismiss(); } private int perse(){ try{ jsonarray ja=new jsonarray(data); jsonobject jo=null; users.clear(); (int i=0;i<ja.length();i++){ jo=ja.getjsonobject(i); string name=jo.getstring("category_name"); string id=jo.getstring("idcategories"); users.add(name); } return 1; } catch (jsonexception e) { e.printstacktrace(); } return 0; } } package info.androidhive.materialtabs.fragments; import android.os.bundle; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.listview; import info.androidhive.materialtabs.r; import info.androidhive.materialtabs.activity.downloader; public class onefragment extends fragment{ button btn_categories; string urladdress="http://192.168.0.107/android/users.php"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view view =inflater.inflate(r.layout.fragment_one, container, false); btn_categories = (button) view.findviewbyid(r.id.btn_categories); final listview lv = (listview) view.findviewbyid(r.id.lv); //final downloader downloader=new downloader(this, lv, urladdress); //downloader.execute(); return view; } }
i assume can correctly retrieve json file server. following code did app.
class loaddataasyn extends asynctask<void,void,void>{ jsonarray data; @override protected void onpreexecute() { super.onpreexecute(); // can use progressdialog indicate request start; } @override protected void doinbackground(void... params) { //user custom class , contactlist return jsonarray data = new user().contactlist(); return null; } @override protected void onpostexecute(void avoid) { super.onpostexecute(avoid); if (data!=null){ if (adapter==null){ adapter = new customadapter(context.this,data); listview.setadapter(adapter); }else{ adapter.refresh(data); } } } }
the idea had in code directly pass json array adapter in custom class:
public class customeradapter extends baseadapter { context context; jsonarray data; public searchschooladapter (context context,jsonarray data){ this.context= context; this.data = data; } @override public int getcount() { if (data!=null){ return data.length(); } return 0; } @override public object getitem(int position) { return position; } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { oneholder holder; if (convertview == null||!(convertview.gettag() instanceof oneholder)){ convertview = layoutinflater.from(context).inflate(r.layout.listrow_layout,null); holder = new oneholder(); holder.image = (imageview)convertview.findviewbyid(r.id.pic); holder.full =(textview)convertview.findviewbyid(r.id.fullname); convertview.settag(holder); }else{ holder = (oneholder)convertview.gettag(); } if (data!=null&&data.length()>position){ try { jsonobject 1 = data.getjsonobject(position); holder.full.settext(one.getstring("name")); } catch (jsonexception e) { e.printstacktrace(); } } return convertview; } class oneholder { imageview image; textview full,abb; } public void refersh(jsonarray data){ this.data=data; notifydatasetchanged(); }
} not know if asking, hope can you.
Comments
Post a Comment