How to clear data in listview and load new data in listview in android on swipe to refresh? -
i have list view add data server in first hit add 5 data , when scroll down add more 5 data server , have swipe refresh on pull down swipe clears data , add 5 data server , again next hit add 5 more same when scroll down .problem right have load more button want replace progress bar @ bottom of listview , when swipe down refresh add more 5 data first time , when on second swipe can't how can archive problem.
code:-
private int m_n_defaultrecordcount = 5;// intiallly record count 5. private int m_n_deafalutlastcount = 0;//initally lastcount 0. private swiperefreshlayout mswiperefresh; private button btnloadmore; private progressbar m_progressbar; private progressdialog m_dialog; @override public void oncreate(@nullable bundle savedinstancestate) { super.oncreate(savedinstancestate); toast.maketext(getactivity(), "login successfully", toast.length_short).show(); } @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { m_main = inflater.inflate(r.layout.deal_listing, container, false);//intialize mainlayout getdetails();// deatail of user sharedpreference...... init();//initialize metho return m_main; } private void getdetails() {// details of user shared preference... cloginsessionmanagement m_osessionmanagement = new cloginsessionmanagement(getactivity());// crating object of login session hashmap<string, string> user = m_osessionmanagement.getlogindetails();// string login session m_szmobilenumber = user.get(cloginsessionmanagement.s_szkey_mobile).trim();// getting password saved preferences.......... m_szencryptedpassword = user.get(cloginsessionmanagement.s_szkey_password).trim();// getting mobile num shared preferences... sz_recordcount = string.valueof(m_n_defaultrecordcount);// increment of record count sz_lastcount = string.valueof(m_n_deafalutlastcount);// increment of last count... s_odataset = new arraylist<>();// making object of arraylist if (networkutil.isconnected(getactivity())) { postdeallistingdatatoserver(); } else { toast.maketext(getactivity(), "please check internet connection !", toast.length_long).show(); } } private void init() {// initialize controls m_progressbar = (progressbar) m_main.findviewbyid(r.id.progressbar1);// finding id of progressview m_progressbar.setvisibility(view.gone);// make profressview invisible first time /*swipe refresh code*/ mswiperefresh = (swiperefreshlayout) m_main.findviewbyid(r.id.activity_main_swipe_refresh_layout); mswiperefresh.setcolorschemeresources(r.color.refresh_progress_1); mswiperefresh.setonrefreshlistener(new swiperefreshlayout.onrefreshlistener() { @override public void onrefresh() { /*here check net connection avialable or not */ if (networkutil.isconnected(getactivity())) { m_listview.removefooterview(btnloadmore); s_odataset.clear(); m_n_deafalutlastcount = 0; sz_lastcount = string.valueof(m_n_deafalutlastcount);// convert int value string ///// swipedata(); } else { toast.maketext(getactivity(), "please check internet connection !", toast.length_long).show(); if (mswiperefresh.isrefreshing()) { mswiperefresh.setrefreshing(false); } } } }); m_n_formimage = new int[]{// defining images in integer array r.drawable.amazon, r.drawable.whatsapp, r.drawable.zorpia, r.drawable.path, r.drawable.app_me, r.drawable.evernote, r.drawable.app_me}; m_listview = (listview) m_main.findviewbyid(r.id.deallist);// findind id of listview m_listview.setfadingedgelength(0); m_listview.setonscrolllistener(new abslistview.onscrolllistener() { @override public void onscrollstatechanged(abslistview view, int scrollstate) { } @override public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) { if (firstvisibleitem == 0) { mswiperefresh.setenabled(true); } else { mswiperefresh.setenabled(false); } } }); } /*this new changes in code ....using volley instead of asynktask*/ /*this method send request server deallisting*/ // method send request server deal list.... public void postdeallistingdatatoserver() { try { string json; // 3. build jsonobject jsonobject jsonobject = new jsonobject();// making object of jsons. jsonobject.put("agentcode", m_szmobilenumber);// put mobile number jsonobject.put("pin", m_szencryptedpassword);// put password jsonobject.put("recordcount", sz_recordcount);// put record count jsonobject.put("lastcountvalue", sz_lastcount);// put last count // 4. convert jsonobject json string json = jsonobject.tostring();// convert json object string system.out.println("request:-" + json); m_dialog = dialogutils.showprogressdialog(getactivity(), "please wait while loading deals..."); jsonobjectrequest jsonobjectrequest = new jsonobjectrequest(request.method.post, cserverapi.m_deallistingurl, jsonobject, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { system.out.println("response:-" + response); m_dialog.dismiss(); try { jsonarray posts = response.optjsonarray("deallist");// deal list in array response (int = 0; < posts.length(); i++) {// loop counting deals server jsonobject post = posts.getjsonobject(i);// counting deal based on index item = new cdealappdatastorage();// creating object of dealappdata storage item.setm_szheadertext(post.getstring("dealname"));// deal name response item.setm_szsubheadertext(post.getstring("dealcode"));// dealcode response item.setm_szdealvalue(post.getstring("dealvalue"));// deal value response item.setm_n_image(m_n_formimage[i]);//set image index wise(dummy) s_odataset.add(item);// add items in arraylist } // loadmore button btnloadmore = new button(getactivity());// creating button btnloadmore.settext("load more deals");// set text in button btnloadmore.setbackgroundresource(r.drawable.button_boarder);// set background resource btnloadmore.settypeface(typeface.default_bold); btnloadmore.settextsize(14); btnloadmore.settextcolor(color.white);// set color of button text btnloadmore.setgravity(gravity.center);// set gravity of button text if (!s_odataset.isempty()) {// condition if data in arraylist not empty // adding load more button lisview @ bottom m_listview.addfooterview(btnloadmore);// add footer in listview m_oadapter = new cdealapplistingadapter(getactivity(), s_odataset);// create adapter object , add arraylist adapter m_listview.setadapter(m_oadapter);//adding adapter recyclerview } else { btnloadmore.setvisibility(view.gone);// else load buttonvisibility set gone } btnloadmore.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) {// load more button onclick listener if (networkutil.isconnected(getactivity())) { m_n_defaultrecordcount = 5;// increment of record count 5 on next load data m_n_deafalutlastcount = m_n_deafalutlastcount + 5;// same here.....as above string itemscount = string.valueof(m_listview.getadapter().getcount()); system.out.println("toatal item:-" + itemscount); sz_recordcount = string.valueof(m_n_defaultrecordcount);// convert int value string sz_lastcount = string.valueof(m_n_deafalutlastcount);// convert int value string ///// loadmoredata(); } else { toast.maketext(getactivity(), "please check internet connection !", toast.length_long).show(); } } }); if (response.getstring("resultdescription").equalsignorecase("connection not available")) {//server based conditions csnackbar.getinstance().showsnackbarerror(m_main.findviewbyid(r.id.mainlayout), "connection lost !", getactivity()); } else if (response.getstring("resultdescription").equalsignorecase("deal list not found")) {// serevr based conditions ..... csnackbar.getinstance().showsnackbarerror(m_main.findviewbyid(r.id.mainlayout), "no more deals available", getactivity()); } else if (response.getstring("resultdescription").equalsignorecase("technical failure")) { csnackbar.getinstance().showsnackbarerror(m_main.findviewbyid(r.id.mainlayout), "technical failure", getactivity()); } } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { system.out.println("errror:-" + error); m_dialog.dismiss(); if (error instanceof timeouterror) { csnackbar.getinstance().showsnackbarerror(m_main.findviewbyid(r.id.activity_main_swipe_refresh_layout), "connection lost ! please try again", getactivity()); } else if (error instanceof networkerror) { csnackbar.getinstance().showsnackbarerror(m_main.findviewbyid(r.id.activity_main_swipe_refresh_layout), "no internet connection", getactivity()); } } }); requestqueue requestqueue = volley.newrequestqueue(getactivity()); requestqueue.add(jsonobjectrequest); } catch (jsonexception e) { e.printstacktrace(); } } /*this method send request server more deals*/ //this method post request server fetch more deal public void loadmoredata() { try { string json; // 3. build jsonobject jsonobject jsonobject = new jsonobject();// making object of jsons. jsonobject.put("agentcode", m_szmobilenumber);// put mobile number jsonobject.put("pin", m_szencryptedpassword);// put password jsonobject.put("recordcount", sz_recordcount);// put record count jsonobject.put("lastcountvalue", sz_lastcount);// put last count // 4. convert jsonobject json string json = jsonobject.tostring();// convert json object string system.out.println("server request:-" + json); m_progressbar.setvisibility(view.visible);// show progress bar jsonobjectrequest jsonobjectrequest = new jsonobjectrequest(request.method.post, cserverapi.m_deallistingurl, jsonobject, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { system.out.println("response:-" + response); try { jsonarray posts = response.optjsonarray("deallist");// getting deal list (int = 0; < posts.length(); i++) { jsonobject post = posts.getjsonobject(i);// getting deal @ position @ item = new cdealappdatastorage();// object create of dealappdatastorage item.setm_szheadertext(post.getstring("dealname"));//getting deal name item.setm_szsubheadertext(post.getstring("dealcode"));// getting deal code item.setm_szdealvalue(post.getstring("dealvalue")); if (!s_odataset.contains(item)) { s_odataset.add(item); m_oadapter.notifydatasetchanged(); } } if (response.getstring("resultdescription").equalsignorecase("connection not available")) {//server based conditions csnackbar.getinstance().showsnackbarerror(m_main.findviewbyid(r.id.mainlayout), "connection lost !", getactivity()); } else if (response.getstring("resultdescription").equalsignorecase("deal list not found")) {// serevr based conditions ..... csnackbar.getinstance().showsnackbarerror(m_main.findviewbyid(r.id.mainlayout), "no more deals available", getactivity()); } else if (response.getstring("resultdescription").equalsignorecase("technical failure")) { csnackbar.getinstance().showsnackbarerror(m_main.findviewbyid(r.id.mainlayout), "technical failure", getactivity()); } } catch (jsonexception e) { e.printstacktrace(); } m_progressbar.setvisibility(view.gone); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { system.out.println("error:-" + error); m_progressbar.setvisibility(view.gone); if (error instanceof timeouterror) { csnackbar.getinstance().showsnackbarerror(m_main.findviewbyid(r.id.activity_main_swipe_refresh_layout), "connection lost ! please try again", getactivity()); } else if (error instanceof networkerror) { csnackbar.getinstance().showsnackbarerror(m_main.findviewbyid(r.id.activity_main_swipe_refresh_layout), "no internet connection", getactivity()); } } }); requestqueue requestqueue = volley.newrequestqueue(getactivity()); requestqueue.add(jsonobjectrequest); } catch (jsonexception e) { e.printstacktrace(); } } /*this method invoke on swipe refresh*/ // method invoke when swipe refresh enable .... public void swipedata() { try { string json; // 3. build jsonobject jsonobject jsonobject = new jsonobject();// making object of jsons. jsonobject.put("agentcode", m_szmobilenumber);// put mobile number jsonobject.put("pin", m_szencryptedpassword);// put password jsonobject.put("recordcount", sz_recordcount);// put record count jsonobject.put("lastcountvalue", sz_lastcount);// put last count // 4. convert jsonobject json string json = jsonobject.tostring();// convert json object string system.out.println("server request:-" + json); jsonobjectrequest jsonobjectrequest = new jsonobjectrequest(request.method.post, cserverapi.m_deallistingurl, jsonobject, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { system.out.println("response:-" + response); mswiperefresh.setrefreshing(false); try { jsonarray posts = response.optjsonarray("deallist");// deal list in array response (int = 0; < posts.length(); i++) {// loop counting deals server jsonobject post = posts.getjsonobject(i);// counting deal based on index item = new cdealappdatastorage();// creating object of dealappdata storage item.setm_szheadertext(post.getstring("dealname"));// deal name response item.setm_szsubheadertext(post.getstring("dealcode"));// dealcode response item.setm_szdealvalue(post.getstring("dealvalue"));// deal value response item.setm_n_image(m_n_formimage[i]);//set image index wise(dummy) s_odataset.add(item);// add items in arraylist } // loadmore button btnloadmore = new button(getactivity());// creating button btnloadmore.settext("load more deals");// set text in button btnloadmore.setbackgroundresource(r.drawable.button_boarder);// set background resource btnloadmore.settypeface(typeface.default_bold); btnloadmore.settextsize(14); btnloadmore.settextcolor(color.white);// set color of button text btnloadmore.setgravity(gravity.center);// set gravity of button text if (!s_odataset.isempty()) {// condition if data in arraylist not empty // adding load more button lisview @ bottom m_listview.addfooterview(btnloadmore);// add footer in listview m_oadapter = new cdealapplistingadapter(getactivity(), s_odataset);// create adapter object , add arraylist adapter m_listview.setadapter(m_oadapter);//adding adapter recyclerview } else { btnloadmore.setvisibility(view.gone);// else load buttonvisibility set gone } btnloadmore.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) {// load more button onclick listener if (networkutil.isconnected(getactivity())) { m_n_defaultrecordcount = 5;// increment of record count 5 on next load data m_n_deafalutlastcount = m_n_deafalutlastcount + 5;// same here.....as above string itemscount = string.valueof(m_listview.getadapter().getcount()); system.out.println("toatal item:-" + itemscount); sz_recordcount = string.valueof(m_n_defaultrecordcount);// convert int value string sz_lastcount = string.valueof(m_n_deafalutlastcount);// convert int value string ///// loadmoredata();// method load more deals server .... } else { toast.maketext(getactivity(), "please check internet connection !", toast.length_long).show(); } } }); if (response.getstring("resultdescription").equalsignorecase("connection not available")) {//server based conditions csnackbar.getinstance().showsnackbarerror(m_main.findviewbyid(r.id.mainlayout), "connection lost !", getactivity()); } else if (response.getstring("resultdescription").equalsignorecase("deal list not found")) {// serevr based conditions ..... csnackbar.getinstance().showsnackbarerror(m_main.findviewbyid(r.id.mainlayout), "no more deals available", getactivity()); } else if (response.getstring("resultdescription").equalsignorecase("technical failure")) { csnackbar.getinstance().showsnackbarerror(m_main.findviewbyid(r.id.mainlayout), "technical failure", getactivity()); } } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { system.out.println("error:-" + error); mswiperefresh.setrefreshing(false); if (error instanceof timeouterror) { csnackbar.getinstance().showsnackbarerror(m_main.findviewbyid(r.id.activity_main_swipe_refresh_layout), "connection lost ! please try again", getactivity()); } else if (error instanceof networkerror) { csnackbar.getinstance().showsnackbarerror(m_main.findviewbyid(r.id.activity_main_swipe_refresh_layout), "no internet connection", getactivity()); } } }); requestqueue requestqueue = volley.newrequestqueue(getactivity()); requestqueue.add(jsonobjectrequest); } catch (jsonexception e) { e.printstacktrace(); } }
}
Comments
Post a Comment