java - android - recycle adapter not appearing in the view pager -
i have tablayout in profileactivity. , 1 of tablayout display recycleview of userlist retrieve database using asynctask. use getter method return userlist asynctask class
my recycleview in activity_user_view.xml , set adapter in class extends fragments. got no error tablayout doesn't appear in view pager.
here code
fragmentfriends.java (class extends fragment)
public class fragmentfriends extends fragment { arraylist<userlist> alist = new arraylist<>(); recyclerview recyclerview; recyclerview.adapter adapter; recyclerview.layoutmanager layoutmanager; @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.activity_user_view,container,false);// activity contains recyclerview phpuserlist phpuserlist = new phpuserlist(getactivity()); phpuserlist.execute(); alist = phpuserlist.getarraylist(); recyclerview = (recyclerview) rootview.findviewbyid(r.id.rvuserlist); layoutmanager = new linearlayoutmanager(getactivity()); recyclerview.setlayoutmanager(layoutmanager); recyclerview.sethasfixedsize(true); adapter = new recycleradapter(alist); recyclerview.setadapter(adapter); return rootview; } }
phpuserlist.java (asynctask class)
public class phpuserlist extends asynctask<void,userlist,void> { private context context; arraylist<userlist> arraylist = new arraylist<>(); phpuserlist(context ctx){ this.context = ctx; } @override protected void doinbackground(void... params) { try { url url = new url("http://njtwicomp.pe.hu/listuser.php"); httpurlconnection httpurlconnection = (httpurlconnection)url.openconnection(); inputstream inputstream = httpurlconnection.getinputstream(); bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(inputstream)); stringbuilder stringbuilder = new stringbuilder(); string line; while ((line = bufferedreader.readline())!=null) { stringbuilder.append(line+"\n"); } httpurlconnection.disconnect(); string json_string = stringbuilder.tostring().trim(); jsonobject jsonobject = new jsonobject(json_string); jsonarray jsonarray = jsonobject.getjsonarray("server_response"); int count = 0; while(count<jsonarray.length()){ jsonobject jo = jsonarray.getjsonobject(count); count++; userlist userlist = new userlist(jo.getstring("name"),jo.getstring("username")); publishprogress(userlist); } log.d("json string", json_string); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (jsonexception e) { e.printstacktrace(); } return null; } @override protected void onpreexecute() { super.onpreexecute(); } @override protected void onprogressupdate(userlist... values) { arraylist.add(values[0]); } @override protected void onpostexecute(void avoid) { super.onpostexecute(avoid); } public arraylist<userlist> getarraylist() { return arraylist; } }
please me
when items added recycler view or list view need notify adapter these changes have occurred aren't doing.
in onprogressupdate
you'd want call adapter.notifyiteminserted(position)
tell recyclerview refresh. you'd need callback or listener in asynctask because doesn't have reference adapter, , shouldn't.
see here notify calls have ability use: https://developer.android.com/reference/android/support/v7/widget/recyclerview.adapter.html
Comments
Post a Comment