7

I am trying to make a wizard using Roman Nurik's library (https://plus.google.com/113735310430199015092/posts/6cVymZvn3f4).

I am having trouble accessing the collected data from the Review Fragment. I made mCurrentReviewItems public in ReviewFragment and then I tried it like this

mNextButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (mPager.getCurrentItem() == mCurrentPageSequence.size()) {
            ReviewFragment reviewFragment = (ReviewFragment)  mPagerAdapter.getItem(mPager.getCurrentItem());

            for (ReviewItem item : reviewFragment.mCurrentReviewItems)
                Log.d(MainActivity.TAG, "Item: " + item.getDisplayValue());
            }

        } else {
            if (mEditingAfterReview) {
                mPager.setCurrentItem(mPagerAdapter.getCount() - 1);
            } else {
                    mPager.setCurrentItem(mPager.getCurrentItem() + 1);
            }
        }
   }
});

However its always null.

Cœur
  • 37,241
  • 25
  • 195
  • 267
urSus
  • 12,492
  • 12
  • 69
  • 89

5 Answers5

11

Inside if (mPager.getCurrentItem() == mCurrentPageSequence.size()) { }

For single page variable:

String data = mWizardModel.findByKey("Sandwich:Bread").getData().getString(Page.SIMPLE_DATA_KEY);

For customized page:

String data = 
mWizardModel.findByKey(THE_KEY).getData().getString(CustomerInfoPage.YOUR_DATA_KEY);

If you want to assign the data back to the wizard, put this at the end of onCreate in FragmentActivity:

Bundle data = new Bundle();
if (!TextUtils.isEmpty(DATA_STRING)) {
    data.putString(Page.SIMPLE_DATA_KEY, DATA_STRING);
    mWizardModel.findByKey("Sandwich:Bread"").resetData(data);
}

The key "Sandwich:Bread" is from the example, change whatever suit you. Never try the multi one, I think it is more or less the same.

Allen Chan
  • 4,041
  • 2
  • 19
  • 16
  • 1
    what happens when you use the same title for multiple pages? – Olayinka Jun 24 '14 at 22:42
  • @Olayinka: the PageList class just iterates over all the pages in an ArrayList until one matches the key. So if you happen to set the same title ( == have the same key) you'll just get the first one that comes up during iteration. Probably not what you'd expect. – FuzzyAmi May 24 '16 at 09:12
3

Sorry for big delay, but I think that someone will found this info useful. I found a way to get all ReviewItems since you can have a lot of branches and you won't be able to use the first answer.

I'm pretty sure, that your mPagerAdapter::getItem code looked like in example (so it just returned new fragment, instead of returning current pager fragment). You have to use instantiateItem to get reference on your ReviewFragment.

Object o = mPager.getAdapter().instantiateItem(mPager, mPager.getCurrentItem());
if(o instanceof ReviewFragment) {
  List<ReviewItem> items = ((ReviewFragment) o).getCurrentReviewItems();
  if(items != null) {
    Log.v(TAG, "Items are: " + items.toString());
  }
}
Anton Shkurenko
  • 4,301
  • 5
  • 29
  • 64
  • Please help Am getting the error cannot resolve method getCurrentReviewItems() from the line List items = ((ReviewFragment) o).getCurrentReviewItems(); above – bmacharia Sep 24 '17 at 08:34
0

This is my code @Anton_Shkurenko

mNextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mPager.getCurrentItem() == mCurrentPageSequence.size()) {
                Object o = mPager.getAdapter().instantiateItem(mPager, mPager.getCurrentItem());
                if(o instanceof ReviewFragment) {
                    List<ReviewItem> items = ((ReviewFragment) o).getCurrentReviewItems();
                    if(items != null) {
                        Log.v(TAG, "Items are: " + items.toString());
                    }
                }
            }
        }
    });
bmacharia
  • 1,026
  • 10
  • 12
0

The best solution is to include this library in your project as module, and implement your own method for getting review items in ReviewFragment.

public List<ReviewItem> getReviewItems() {
    return mCurrentReviewItems;
}

I am not sure why developer did not add that. It's the most important thing in project. Choose items and DO something with them.

vanste25
  • 1,754
  • 14
  • 39
0

Anyone still looking for a solution for this issue you can use following code

    ArrayList<ReviewItem> reviewItems = new ArrayList<ReviewItem>();
    for (Page page : mWizardModel.getCurrentPageSequence()) {
        page.getReviewItems(reviewItems);
    }