android - How to restart activity with data of previous or next position of a ListView -


i'm building book reader app , having issues on how move next or previous chapter.

the fragment in charge of showing list of chapter (basically listview). capture item onitemclick:

listview.setonitemclicklistener(new adapterview.onitemclicklistener() {         @override         public void onitemclick(adapterview<?> adapterview, view view, int i, long l) {             intent intent = new intent(getactivity().getapplicationcontext(),readeractivity.class);             intent.putextra("name",adapter.getitem(i).getname());             intent.putextra("url",adapter.getitem(i).geturl());             startactivity(intent);         } }); 

it start readeractivity , display content of book. readeractivity has 2 buttons moving next/previous chapter. how can data (the name , url) listview?

at moment i'm thinking of 2 methods:

  1. calling finish() on readeractivity , override ondestroy() method call method next/previous data in fragment holds listview.

  2. passing whole arraylist of chapters readeractivity can retrieve item wants (probably not solution since there many chapters).

is there better way?

there way many ways best one.

some of them:

  1. use application.java share in-memory data (data stores) place. example:

    public final application extends android.app.application {     private list<chapter> chapters;      public void setchapters(list<chapter> chapters) {         this.chapters = chapters != null ? chapters : collections.<chapter>epmtylist();     }      public list<chapter> chapters() {         return chapters;     } }  // place has context (or context) list<chapter> chapters = ((application) getcontext().getapplicationcontext()).getchapters(); 
  2. make list static member public static getter , reference readeractivity

    public final applicationactivity extends activity {     private static list<chapter> chapters;      ....      public static list<chapter> getchapters() {         return chapters; } 
  3. store list in shared preferences activity can reference it

  4. include list of chapters in intent extras , pass readeractivity

i think readeractivity needs deal whole book rather single chapter. means approach (4) candidate.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -