android - Maintain the scroll position of a listview after getting new data -
i have listview after refreshing , getting new data. goes top of listview , not last scrolled to.
i have set timer after 2 seconds, gets new data , puts listview.
any appreciated. in advance :d
here mainactivity
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); listview = (listview) findviewbyid(r.id.listview); listview.setonitemclicklistener(this); getjson(); handler.postdelayed(runnable, 3500); } private runnable runnable = new runnable() { @override public void run() { getjson(); handler.postdelayed(this, 2000); } }; private void showemployee(){ jsonobject jsonobject = null; arraylist<hashmap<string,string>> list = new arraylist<hashmap<string, string>>(); try { jsonobject = new jsonobject(json_string); jsonarray result = jsonobject.getjsonarray(config.tag_json_array); for(int = 0; i<result.length(); i++){ jsonobject jo = result.getjsonobject(i); string id = jo.getstring(config.tag_id); string name = jo.getstring(config.tag_name); hashmap<string,string> employees = new hashmap<>(); employees.put(config.tag_id,id); employees.put(config.tag_name,name); list.add(employees); } } catch (jsonexception e) { e.printstacktrace(); } listadapter adapter = new simpleadapter( mainactivity.this, list, r.layout.list_item, new string[]{config.tag_id,config.tag_name}, new int[]{r.id.id, r.id.name}); listview.setadapter(adapter); } private void getjson(){ class getjson extends asynctask<void,void,string> { @override protected void onpreexecute() { super.onpreexecute(); } @override protected void onpostexecute(string s) { super.onpostexecute(s); json_string = s; showemployee(); } @override protected string doinbackground(void... params) { requesthandler rh = new requesthandler(); string s = rh.sendgetrequest(config.url_get_all); return s; } } getjson gj = new getjson(); gj.execute(); }
in oncreate() initialize adapter empty list , bind listview. make adapter , list global in class.
now whenever add data adapter call adapter.notifydatasetchanged()
this how works in fragment.
Comments
Post a Comment