android - addOnScrollListener, pass base and limit -
i learning how use onscrolllistner
the recyclerokhttphandler
class execute select base 0 , limit 5 server. want execute again recyclerokhttphandler
new data , examle base 5 limit 10. when adding below on onscrolllistner
handler.execute().get();
i got error :
cannot execute task: task has been executed (a task can executed once)
ok understand cannot execute again task , how should passe base , limit ?
this (it working) execute class images server, need pass base 0 , limit 5
final recyclerokhttphandler handler = new recyclerokhttphandler( this, new recyclerokhttphandler.myinterface() { @override public void mymethod(arraylist result) { madapter_first = new myadapter(result,searchactivity.this); madapter_first.notifydatasetchanged(); mrecyclerview_first.setadapter(madapter_first); } },"girls",base,limit); try { handler.execute().get(); } catch (exception e) { log.d("searchactivity error", "error in mrecyclerview_first"); e.printstacktrace(); }
and addonscrolllistener,
mrecyclerview_first.addonscrolllistener(new recyclerview.onscrolllistener() { @override public void onscrolled(recyclerview recyclerview, int dx, int dy) { super.onscrolled(recyclerview, dx, dy); visibleitemcount = mrecyclerview_first.getchildcount(); totalitemcount = mlayoutmanager.getitemcount(); firstvisibleitem = mlayoutmanager.findfirstvisibleitemposition(); if (loading) { if (totalitemcount > previoustotal) { loading = false; previoustotal = totalitemcount; system.out.println("previoustotal:" + previoustotal); system.out.println("totalitemcount:" + totalitemcount); system.out.println("visibleitemcount:" + visibleitemcount); } } if (!loading && (totalitemcount - visibleitemcount) <= (firstvisibleitem + visiblethreshold)) { // end has been reached system.out.println(totalitemcount); log.i("yaeye!", "end called"); // base =string.valueof(firstvisibleitem); // limit=string.valueof(visibleitemcount); // try { handler.execute().get(); } catch (exception e) { log.d("searchactivity error", "error in mrecyclerview_first"); e.printstacktrace(); } loading = true; } } });
this class recyclerokhttphandler
public class recyclerokhttphandler extends asynctask<string, void, string> { private context mcontext; private myinterface mlistener; public string category; public string basestart; public string limitend; public recyclerokhttphandler(context context, myinterface mlistener, string categ, string base, string limit){ mcontext = context; this.mlistener = mlistener; category=categ; basestart=base; limitend=limit; } public interface myinterface { public void mymethod(arraylist result); } private final string fetch_url = "http://justedhak.com/old-files/recyclerview_data.php"; // arraylist<listitem> listitem; arraylist<categorylist> listitem; int resulta; okhttpclient httpclient = new okhttpclient(); listview list; string myjson; jsonarray peoples = null; inputstream inputstream = null; @override protected string doinbackground(string... params) { log.d("okhttp fetch_url", fetch_url); requestbody formbody = new formencodingbuilder() .add("category", category) .add("base", basestart) .add("limit", limitend) .build(); request request = new request.builder() .url(fetch_url) .post(formbody) .build(); string result = null; try { response response = httpclient.newcall(request).execute(); if (!response.issuccessful()) throw new ioexception("unexpected code " + response); inputstream = response.body().bytestream(); bufferedreader reader = new bufferedreader(new inputstreamreader(inputstream, "utf-8"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } result = sb.tostring(); resulta = 1; //"success // return response.body().bytes(); } catch (exception e) { toast.maketext(mcontext, "connection failed, check connection", toast.length_long).show(); e.printstacktrace(); } { try{if(inputstream != null)inputstream.close();}catch(exception squish){} } return result; } @override protected void onpostexecute(string result){ if( resulta ==1){ myjson=result; log.e("result",result); showlist(); } else{ log.e("d","there error on postexecute in okhhttphandler.java"); } } protected void showlist(){ try { jsonobject jsonobj = new jsonobject(myjson); peoples = jsonobj.getjsonarray("result"); system.out.println("length:"+peoples.length()); int j_length=peoples.length()-1; //jsonobject maxj = peoples.getjsonobject(peoples.length() - 1); // max of arrray jsonobj= peoples.getjsonobject(j_length); string j_id= jsonobj.getstring("id"); int _id = integer.parseint(j_id); system.out.println(j_id); //max of databasehandler db = new databasehandler(mcontext); string db_id=""; db_id = db.getmax(); if (db_id== null) { db_id="0"; } int d_id = integer.parseint(db_id); log.e("db_id", db_id); log.e("j_id",j_id); // if (_id < d_id) { system.out.println("getting json result "); listitem = new arraylist<categorylist>(); (int = 0; < peoples.length(); i++) { jsonobject c = peoples.getjsonobject(i); string id = c.getstring("id"); string url = c.getstring("url"); listitem.add(new categorylist(id, url)); } if (mlistener != null) mlistener.mymethod(listitem); } catch (jsonexception e) { e.printstacktrace(); } } }
i read problem again, , solution might create new recyclerokhttphandler each time. that's error message says , that's how asynctasks work. if call .execute() once cannot call again, have create new object.
still should override onpostexecute() else blocking ui thread. have @ asynctask. need code recyclerokhttphandler more...
Comments
Post a Comment