java - Create a List with next and previous in Android Sutdio -
i have class person 2 attributes : string firstname string lastname
i have arraylist of person , want display in list custom adapter.
but don't want use listview, want display 5 item in each page possibility of link button previous , next.
when click on next 5 person of arraylist appear. etc... tried make fragments didn't go well.
how can it?
thanks help
here solution listview activity_navigation_list.xml
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <linearlayout android:id="@+id/data_list" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginbottom="48dp" android:orientation="vertical"/> <button android:id="@+id/btn_previous" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="previous" android:layout_gravity="bottom|left"/> <button android:id="@+id/btn_next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="next" android:layout_gravity="bottom|right"/> </framelayout> import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.util.log; import android.view.view; import android.widget.button; import android.widget.linearlayout; import android.widget.textview; import org.json.jsonarray; import org.json.jsonobject; import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import java.util.arraylist; import java.util.list; import in.ashish29agre.stackoverflow.r; public class listnavigationandroid extends appcompatactivity implements view.onclicklistener { private list<string> nameslist; private button btnprevious; private button btnnext; private int navindex = 0; private linearlayout listview; int currentpage = -1; int pagesize = 5; // private int originalsize; // int currentindex = 0; private int from; private int to; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_navigation_list); nameslist = new arraylist<>(); btnprevious = (button) findviewbyid(r.id.btn_previous); btnprevious.setonclicklistener(this); btnnext = (button) findviewbyid(r.id.btn_next); btnnext.setonclicklistener(this); listview = (linearlayout) findviewbyid(r.id.data_list); loadnamesfromassets(); loadnextset(); } private void loadnamesfromassets() { try { stringbuilder buf = new stringbuilder(); inputstream json = getassets().open("names.json"); bufferedreader in = new bufferedreader(new inputstreamreader(json, "utf-8")); string str; while ((str = in.readline()) != null) { buf.append(str); } in.close(); log.d("data", "" + buf.tostring()); jsonarray jsonarray = new jsonarray(buf.tostring()); (int = 0; < jsonarray.length(); i++) { jsonobject object = jsonarray.getjsonobject(i); nameslist.add(object.getstring("name")); } } catch (exception e) { e.printstacktrace(); } } @override public void onclick(view view) { switch (view.getid()) { case r.id.btn_previous: loadpreviousset(); break; case r.id.btn_next: loadnextset(); } } private void loadnextset() { try { ++currentpage; listview.removeallviews(); updatepaginationparams(); list<string> sublist = nameslist.sublist(from, to); (string name : sublist) { textview textview = new textview(this); textview.settext(name); listview.addview(textview); } } catch (exception e) { e.printstacktrace(); } } private void loadpreviousset() { if (currentpage <= 0) { return; } --currentpage; listview.removeallviews(); updatepaginationparams(); list<string> sublist = nameslist.sublist(from, to); (string name : sublist) { textview textview = new textview(this); textview.settext(name); listview.addview(textview); } } private void updatepaginationparams() { = math.max(0, currentpage * pagesize); = math.min(nameslist.size(), (currentpage + 1) * pagesize); } }
put file in assets directory: names.json
[ { "name": "blackwell moses" }, { "name": "mclaughlin nolan" }, { "name": "molina hyde" }, { "name": "drake casey" }, { "name": "frances solis" }, { "name": "rocha douglas" }, { "name": "lelia holmes" }, { "name": "helga maynard" }, { "name": "robyn michael" }, { "name": "malone tate" }, { "name": "aida stein" }, { "name": "mcknight richards" }, { "name": "ethel harmon" }, { "name": "kelly ferguson" }, { "name": "ophelia clemons" }, { "name": "blanchard potts" }, { "name": "eula golden" }, { "name": "eugenia suarez" }, { "name": "daniel woodard" }, { "name": "mary deleon" }, { "name": "bean arnold" }, { "name": "shirley zimmerman" }, { "name": "fox cox" }, { "name": "delgado ashley" }, { "name": "rae rosario" }, { "name": "cortez randolph" }, { "name": "bruce baird" }, { "name": "hinton reed" } ]
Comments
Post a Comment