Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go to spine reference programatically #106

Open
Redman1037 opened this issue Jan 23, 2017 · 3 comments
Open

Go to spine reference programatically #106

Redman1037 opened this issue Jan 23, 2017 · 3 comments

Comments

@Redman1037
Copy link

Redman1037 commented Jan 23, 2017

How can i go to spine reference programatically ? I am getting titles of the spine and displaying it in list view ,now when user clicks on list item i want to go to that perticular page(chapter) programatically.How can i acheive this?

@ashleypeacock
Copy link

  try {
            for (SpineReference r : spine.getSpineReferences()) {
                BufferedReader ris = new BufferedReader(new InputStreamReader(r.getResource().getInputStream()));
                StringBuffer buffer = new StringBuffer();
                String s;
                while ((s = ris.readLine()) != null) {
                    buffer.append(s);
                }
                String result = new String(buffer);
// print result here = page html
               
            }
        } catch(Exception e) {
            Log.e(TAG, "getText: ", e);
        }

Will go through all of the spine references and get the pages html.

@Redman1037
Copy link
Author

@ashleypeacock I know that ,but after loading all the pages in webview, i want to navigate to a spine reference point programatically how can i acheive it? for example in spine if there is chapter 1 or chapter 2 ,how can i go to that specific point in book?

@ashleypeacock
Copy link

ashleypeacock commented Feb 21, 2017

Well, you just need to replace the 'for loop ' above with whatever index you like.

SpineReference r = book.getSpine().getSpineReferences().get(number);

Keep in mind, the first few pages are usually things like title pages or cover pages, about pages, etc. So it's harder to say "Get me chapter 1".

You could also use the table of contents in replacement of the spine reference.

public String getChapterText(Book book, int chapterNumber) {
        TOCReference requiredChapter = book.getTableOfContents().getTocReferences().get(chapterNumber);
        try {
            BufferedReader ris = new BufferedReader(new InputStreamReader(requiredChapter.getResource().getInputStream()));
            StringBuffer buffer = new StringBuffer();
            String s;
            while ((s = ris.readLine()) != null) {
                buffer.append(s);
            }
            String result = new String(buffer);
            return result;
        }catch(IOException io) {
            Log.e(TAG, "getChapterText: ", io);
        }
        return "";
    }

And by doing this you could get a chapter by it's name, which might be better than getting it by index.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants