Roman Nurik's Wizard pager - how to access col

2019-03-20 07:36发布

问题:

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.

回答1:

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.



回答2:

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());
  }
}


回答3:

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());
                    }
                }
            }
        }
    });


回答4:

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.



回答5:

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);
    }