android - How can I display a listview from a database in a fragment in a drawer navigation activity -
can me please display listview database in fragment (drawer navigation activity)? tried doesn't work in fragment.
this's file fragment3.java
public class fragment3 extends fragment { private string json_string; private listview listview; public static final string url="http://10.0.3.2/scripts/select.php"; public static final string tag_json_array="result"; @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.fragment3, container, false); super.oncreate(savedinstancestate); setcontentview(r.layout.fragment3); listview = (listview) findviewbyid(r.id.listview); listview.setonitemclicklistener(this); getjson(); } private void display(){ jsonobject jsonobject = null; arraylist<hashmap<string,string>> list = new arraylist<hashmap<string, string>>(); try { jsonobject = new jsonobject(json_string); jsonarray result = jsonobject.getjsonarray(tag_json_array); for(int = 0; i<result.length(); i++){ jsonobject jo = result.getjsonobject(i); string id= jo.getstring("id"); string name = jo.getstring("name"); string email= jo.getstring("email"); hashmap<string,string> liste= new hashmap<>(); // liste.put("type",type); liste.put("id",id); liste.put("email",email); list.add(liste); } } catch (jsonexception e) { e.printstacktrace(); } listadapter adapter = new simpleadapter( fragment3.this, list, r.layout.listview, new string[]{"id","email"}, new int[]{r.id.id, r.id.name}); listview.setadapter(adapter); } private void getjson(){ class getjson extends asynctask<void,void,string> { progressdialog loading; @override protected void onpreexecute() { super.onpreexecute(); loading = progressdialog.show(fragment3.this,"fetching data","wait...",false,false); } @override protected void onpostexecute(string s) { super.onpostexecute(s); loading.dismiss(); json_string = s; display(); } @override protected string doinbackground(void... params) { requesthandler rh = new requesthandler(); string s = rh.sendgetrequest(url); return s; } } getjson gj = new getjson(); gj.execute(); } @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { intent intent = new intent(this, viewuser.class); hashmap<string,string> map =(hashmap)parent.getitematposition(position); string tid= map.get("id").tostring(); intent.putextra("type", tid); startactivity(intent); }
} in advance
Comments
Post a Comment