0

I know this question has been asked on stackoverflow, (Link To Original Question)

I was following the solution provided there, and got a few errors. (I wanted to ask in comment there only, but think I don't have enough reputation to ask in comment of a marked answer) My original question is same, i.e. "I want to paginate long text"

Errors encountered in the provided answer(link posted above) - 1. After telling about PageSplitter class, solution provider (mixel) stated this -

...Then by using PageSplitter.getPages() method you can get original text splitted to pages and put each of them into TextView:

pageView.setAdapter(new TextPagerAdapter(getSupportFragmentManager(), pageSplitter.getPages()));

I think here he meant pagesView. (my real concern is written below, mentioning it just in case i missed something else.)

  1. This is the real problem I am facing - In the PageFragment class, I am getting this error -

The method getObjectStorage() is undefined for the type PageFragment

while Mixel states about getObjectStorage() -

getObjectStorage() returns singleton SharedObjectStorage that stores map from keys to weak references to objects (this is useful when you want to put custom object to Bundle):

and I have created a separate class named SharedObjectStorage, as mentioned by Mixel, but I am still getting the error.

PageFragment

public class PageFragment extends Fragment {
    private final static String PAGE_TEXT = "PAGE_TEXT";

    public static PageFragment newInstance(CharSequence pageText) {
        PageFragment frag = new PageFragment();
        Bundle args = new Bundle();
        args.putLong(PAGE_TEXT, getObjectStorage().putSharedObject(pageText));
        frag.setArguments(args);
        return frag;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        CharSequence text = (CharSequence) getObjectStorage().getSharedObject(getArguments().getLong(PAGE_TEXT));
        TextView pageView = (TextView) inflater.inflate(R.layout.page, container, false);
        pageView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.text_size));
        pageView.setText(text);
        return pageView;
    }
}

SharedObjectStorage

public class SharedObjectStorage {
    private final Random random = new Random(Calendar.getInstance().getTimeInMillis());
    private HashMap<Long, WeakReference<Object>> sharedObjects = new HashMap<Long, WeakReference<Object>>();

    public synchronized Long putSharedObject(Object o) {
        long key;
        do {
            key = random.nextLong();
        } while (sharedObjects.containsKey(key));

        sharedObjects.put(key, new WeakReference<Object>(o));
        return key;
    }

    public synchronized Object getSharedObject(long key) {
        if (sharedObjects.containsKey(key)) {
            return sharedObjects.get(key).get();
        }
        return null;
    }
}

Is there anyway to work around this?

p.s. - I didn't post the answer here, as it's already on stackoverflow itself. But, if you still ask me to, I will. Thanks.

Community
  • 1
  • 1
Dr. Atul Tiwari
  • 1,085
  • 5
  • 22
  • 46
  • did you find solution to this? – Khalid ElSayed Aug 27 '14 at 23:35
  • Hi guy, currently I also have this problem, did you have the solution now? – Liping Huang Sep 17 '14 at 02:46
  • @KhalidElSayed Sorry I have not found any working solution so far. I think with some tweak this answer [link](http://stackoverflow.com/questions/20204348/how-to-break-styled-text-into-pages-in-android) should be working, but I dont have enough reputation to comment on that answer. – Dr. Atul Tiwari Sep 17 '14 at 06:34
  • @LeoHuang I have not found any working solution so far. I think with some tweak this answer [link](http://stackoverflow.com/questions/20204348/how-to-break-styled-text-into-pages-in-android) should be working, but I dont have enough reputation to comment on that answer. Please let me know, if you could find a way. thanks. – Dr. Atul Tiwari Sep 17 '14 at 06:35
  • I just asked the question at the original post. But how did u guys setup R.id.pages? – hugocarlmartin Sep 24 '14 at 09:51
  • He just updated his post. https://github.com/mxl/pagesplitter – hugocarlmartin Sep 25 '14 at 18:12

0 Answers0