Update ListView Record Duplicated Android -
i put limit of 15 records displayed in listview. 15 records displayed when clicked on load more button, add previous 15 records , new records too. here async task more records being added. use articlesadapter.notifydatasetchanged(); set issue remains. believe has articlesfiltered.size()/15)+1.
public class actusscreen extends baseactivity implements onclicklistener { private displayimageoptions options; imageloader imageloader; string link; //... final int none=0; final int theme=1; final int search=2; final int theme_to_search=3; final int search_results=4; final int theme_to_search_results=5; int mode=none; public arraylist<article> articles; public arraylist<article> articlesfiltered; public arraylist<theme> themes; public arraylist<theme> themefiltered; public static int titleindex=0; static boolean original_view = false; relativelayout adlayout; listview themeslist; relativelayout searchlayout; edittext searchfield; button back, theme; stringbuilder builder; scrollview scrollme; themesadapter themeadapter; articlesadapter articlesadapter; textview header_text; actusscreen context; progressdialog pd; imageview image_actus; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.actus); context=this; back=(button)findviewbyid(r.id.envoye); back.setonclicklistener(this); back.setvisibility(view.gone); theme=(button)findviewbyid(r.id.theme); theme.setonclicklistener(this); button search=(button)findviewbyid(r.id.search); search.setonclicklistener(this); header_text = (textview)findviewbyid(r.id.text_titre); header_text.settext(getresources().gettext(r.string.actus).tostring()); adlayout=(relativelayout)findviewbyid(r.id.adlayout); themeadapter=new themesadapter(this); themeslist=(listview)findviewbyid(r.id.themeslist); themeslist.setadapter(themeadapter); themeslist.setvisibility(view.gone); selectedarticle.mtypo=1; articlesadapter=new articlesadapter(this); listview articleslist=(listview)findviewbyid(r.id.articleslist); articleslist.setadapter(articlesadapter); searchlayout=(relativelayout)findviewbyid(r.id.searchlayout); searchlayout.setvisibility(view.gone); searchfield=(edittext)findviewbyid(r.id.keyword); button valid=(button)findviewbyid(r.id.valid); valid.setonclicklistener(this); new getallarticlestask().execute(); image_actus = (imageview)findviewbyid(r.id.image); image_actus.setonclicklistener(new onclicklistener(){ @override public void onclick(view v) { // todo auto-generated method stub string url = link; intent = new intent(intent.action_view); i.setdata(uri.parse(url)); startactivity(i); } }); options = new displayimageoptions.builder() // .showstubimage(r.drawable.ic_launcher) .displayer(new roundedbitmapdisplayer(3)) .cacheinmemory() .cacheondisc() .build(); imageloader = imageloader.getinstance(); imageloader.init(imageloaderconfiguration.createdefault(getapplicationcontext())); new backtask().execute(""); } @override protected void onstart() { // todo auto-generated method stub //favoritescreen.flagx=1; super.onstart(); } @override public void onclick(view v) { switch(v.getid()){ case r.id.envoye: back(); break; case r.id.theme: if(themeslist.getvisibility()==view.visible) themeslist.setvisibility(view.gone); else themeslist.setvisibility(view.visible); break; case r.id.search: search(); break; case r.id.valid: searchlayout.setvisibility(view.gone); if(mode==search) mode=search_results; else mode=theme_to_search_results; filterarticles(); break; } } public void search(){ switch(mode){ case search_results: searchlayout.setvisibility(view.visible); mode=search; break; case theme_to_search_results: searchlayout.setvisibility(view.visible); mode=theme_to_search; break; case theme: searchlayout.setvisibility(view.visible); mode=theme_to_search; break; case none: searchlayout.setvisibility(view.visible); back.setvisibility(view.visible); theme.setvisibility(view.gone); mode=search; break; } } public void back(){ switch(mode){ case search_results: searchlayout.setvisibility(view.visible); mode=search; break; case theme_to_search_results: searchlayout.setvisibility(view.visible); mode=theme_to_search; break; case theme: back.setvisibility(view.gone); theme.setvisibility(view.visible); /* * intent intent = getintent(); finish(); startactivity(intent); */ articlesadapter=new articlesadapter(this); listview articleslist=(listview)findviewbyid(r.id.articleslist); articleslist.setadapter(articlesadapter); //actusscreen.titleindex=0; articlesadapter.mode = true; titleindex=0; new getallarticlestask().execute(); header_text.settext(getresources().gettext(r.string.actus).tostring()); mode=none; break; case search: searchlayout.setvisibility(view.gone); back.setvisibility(view.gone); theme.setvisibility(view.visible); mode=none; copyarticles(); articlesadapter.notifydatasetchanged(); break; case theme_to_search: searchlayout.setvisibility(view.gone); mode=theme; copyarticles(); articlesadapter.notifydatasetchanged(); break; } } private void copyarticles() { if(articles!=null){ if(articlesfiltered==null) articlesfiltered=new arraylist<article>(); else articlesfiltered.clear(); for(article a: articles) articlesfiltered.add(a); } } public void filterarticles(){ string key=searchfield.gettext().tostring().tolowercase(); if(key.length()>0){ if(articlesfiltered!=null){ articlesfiltered.clear(); system.gc(); } for(article a: articles){ if(a.name.tolowercase().contains(key)) articlesfiltered.add(a); } articlesadapter.notifydatasetchanged(); } } private class getallarticlestask extends asynctask<void, void, void> { @override public void doinbackground(void... params) { if(globals.themes==null) globals.themes=httpfunctions.getthemeslist(); if(articles!=null){ articles.clear(); system.gc(); } articles=httpfunctions.getallarticles(); copyarticles(); return null; } @override public void onpreexecute() { pd = progressdialog.show(context, "", context.getresources().getstring(r.string.loading), true, false); } @override public void onpostexecute(void result) { pd.dismiss(); articlesadapter.notifydatasetchanged(); themeadapter.notifydatasetchanged(); } } private class getthemearticlestask extends asynctask<string, void, void> { @override public void doinbackground(string... params) { if(articles!=null){ articles.clear(); system.gc(); } articles=httpfunctions.getthemearticles(params[0]); //begin 06/03; articles.theme_id not set httpfunctions when specific theme selected; need set explicitly (article : articles) a.theme_id=params[0]; //end system.out.println("theme article: "+ httpfunctions.getthemearticles(params[0])); copyarticles(); return null; } @override public void onpreexecute() { pd = progressdialog.show(context, "", context.getresources().getstring(r.string.loading), true, false); } @override public void onpostexecute(void result) { pd.dismiss(); articlesadapter.notifydatasetchanged(); } } private class getmorearticlestask extends asynctask<void, void, void> { @override public void doinbackground(void... params) { arraylist<article>morearticles=httpfunctions.getmorearticles(addone((articlesfiltered.size()/15))); if(morearticles!=null){ articles.addall(articles); copyarticles(); } return null; } @override public void onpreexecute() { pd = progressdialog.show(context, "", context.getresources().getstring(r.string.loading), true, false); } @override public void onpostexecute(void result) { pd.dismiss(); articlesadapter.notifydatasetchanged(); } } public void articleselected(int id){ globals.copyarticles(articlesfiltered); intent i=new intent(context, selectedarticle.class); i.putextra("id", id); //begin //i.putextra("title", articlesadapter.selected); if (titleindex==0){ string title=util.gettitlename(articlesfiltered.get(id).type, articlesfiltered.get(id).theme_id); i.putextra("title", title); } else{ string title=articlesadapter.selected.touppercase(); i.putextra("title", title); } //end context.startactivity(i); } public void themeselected(int id){ themeslist.setvisibility(view.gone); mode=theme; theme.setvisibility(view.gone); back.setvisibility(view.visible); articlesadapter.mode = false; //begin 06/03 //articlesadapter.selected=globals.themes.get(id).name; //header_text.settext(globals.themes.get(id).name.touppercase()); spanned name=html.fromhtml(globals.themes.get(id).name); system.out.println("spanned name: "+ name); articlesadapter.selected=name.tostring().touppercase(); header_text.settext(name.tostring().touppercase()); //end system.out.println("theme_name: "+ globals.themes.get(id).name); new getthemearticlestask().execute(globals.themes.get(id).id); } public void loadmore(){ new getmorearticlestask().execute(); } @override protected void onresume() { // todo auto-generated method stub if (original_view==true){ new getallarticlestask().execute(); theme.setvisibility(view.visible); back.setvisibility(view.gone); header_text.settext(getresources().gettext(r.string.actus).tostring()); original_view=false; } super.onresume(); } @override protected void onpause() { // todo auto-generated method stub selectedreglementation.setview=true; super.onpause(); } class backtask extends asynctask<string, void, arraylist<post>> { @override protected void onpreexecute() { // todo auto-generated method stub super.onpreexecute(); } @override protected arraylist<post> doinbackground(string... urls) { arraylist<post> newpostarraylist=new arraylist<post>(); try{ url url = new url(""); bufferedreader reader = null; builder = new stringbuilder(); try { reader = new bufferedreader(new inputstreamreader(url.openstream(), "utf-8")); (string line; (line = reader.readline()) != null;) { builder.append(line.trim()); } } { if (reader != null) try { reader.close(); } catch (ioexception logorignore) {} } system.out.println("builder: "+ builder); }catch(exception ex){} return newpostarraylist; } @override protected void onpostexecute(arraylist<post> result) { string[] banner_image = builder.tostring().split(";"); imageloader.displayimage(banner_image[2], image_actus,options); link = banner_image[1]; } } public int addone(int i){ return i+1; } }
and article adapter
public class articlesadapter extends baseadapter { actusscreen main; imageloader imageloader; string imageurl=""; public static string selected=""; public static boolean mode=false; int x=0; //disable a.theme_id null pointer wh public static int bine=0; public articlesadapter(actusscreen m) { main=m; imageloader=new imageloader(m); } public int getcount() { if(main.articlesfiltered!=null) return main.articlesfiltered.size(); return 0; } public object getitem(int position) { return null; } public long getitemid(int position) { return 0; } public view getview(final int position, view convertview, viewgroup parent) { final viewholder holder; if (convertview==null) { convertview=newview(position, parent); holder = new viewholder(); holder.image=(imageview)convertview.findviewbyid(r.id.image); holder.remove=(imageview)convertview.findviewbyid(r.id.remove); holder.title=(textview)convertview.findviewbyid(r.id.title); holder.text_title=(textview)convertview.findviewbyid(r.id.text_title); holder.more=(relativelayout)convertview.findviewbyid(r.id.morelayout); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } article a=main.articlesfiltered.get(position); imageloader.displayimage(string.format(imageurl, a.image), holder.image); string str = a.name; int length = str.length(); string newstr = a.name; if (length>65) newstr = str.replaceall("^(.{74})(.*)$","$1..."); holder.title.settext(html.fromhtml(newstr)); holder.text_title.settext(selected.touppercase()); holder.remove.setvisibility(view.gone); if (holder.text_title.equals(null)) holder.text_title.settext("t"); if(position==main.articlesfiltered.size()-1 && main.articlesfiltered.size()<=45 && main.articlesfiltered.size()%15==0) holder.more.setvisibility(view.visible); else holder.more.setvisibility(view.gone); convertview.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { main.articleselected(position); selectedarticle.mtypo=1; } }); holder.more.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { main.loadmore(); } }); if (actusscreen.titleindex==0){ ... } } return(convertview); } private view newview(int position, viewgroup parent) { return(main.getlayoutinflater().inflate(r.layout.articles_row, parent, false)); } class viewholder { imageview image, remove; textview title; textview text_title; relativelayout more; } }
update , httpfunction getmorearticles()
public static arraylist<article> getmorearticles(int page){ string url=last_article_url; if(url.endswith(".php")) url+="?page="+page; else url+="&page="+page; string response=getresponse(url); if(!error.equals(response)){ return jsonparsingfunctions.parsearticles(response); } return null; }
what happening concatenation in method call. articlesfiltered.size()/15 giving 1, +1 not adding 1, concatenating 1, i.e 1+1 = 11 not 2. create method
public int addone(int i){ return i+1; }
and use
arraylist<article>morearticles=httpfunctions.getmorearticles(addone((articlesfiltered.size()/15)));
that should work providing rest of code good
Comments
Post a Comment