0

I am trying to create an ArrayAdapter similar to the sample shown in the readme at this github page https://github.com/commonsguy/cwac-pager. I have a variety of Fragments that I want to pass into an ArrayPagerAdapter instead of just one like in the example. I'm confused on how desc.getTitle is supposed to be passed in into the newly created instance of the Fragment in the example. Also how would I be able to do this same thing using android.app.Fragment to create a new instances of Fragments? I have posted my code below I have an error on newInstance();

static class SimplePagerAdapter extends ArrayPagerAdapter<android.app.Fragment>{



    public SimplePagerAdapter(FragmentManager fragmentManager,
                              ArrayList<PageDescriptor> descriptors) {
        super(fragmentManager, descriptors);
    }

    @Override
    protected android.app.Fragment createFragment(PageDescriptor desc) {

        Fragment newFragment = new JudgeMainFragment();

        return(android.app.Fragment.newInstance(desc.getTitle()));
    }
}
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Blaine
  • 31
  • 6

1 Answers1

0

I'm confused on how desc.getTitle is supposed to be passed in into the newly created instance of the Fragment in the example

Your code is different from what I have in the project README and will not work, as Fragment has no newInstance() method. The README is showing an EditorFragment, specifically the EditorFragment from the sample app. EditorFragment implements a newInstance() method, using the arguments Bundle to get the title into the fragment:

package com.commonsware.cwac.pager.demo;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class EditorFragment extends Fragment {
  private static final String KEY_TITLE="title";

  static EditorFragment newInstance(String title) {
    EditorFragment frag=new EditorFragment();
    Bundle args=new Bundle();

    args.putString(KEY_TITLE, title);
    frag.setArguments(args);

    return(frag);
  }

  @Override
  public View onCreateView(LayoutInflater inflater,
                           ViewGroup container,
                           Bundle savedInstanceState) {
    View result=inflater.inflate(R.layout.editor, container, false);
    EditText editor=(EditText)result.findViewById(R.id.editor);

    editor.setHint(getTitle());

    return(result);
  }

  public String getTitle() {
    return(getArguments().getString(KEY_TITLE));
  }
}

Note that there is no requirement for the fragment to know its title, but if you want the fragment to know its title, use the arguments Bundle, such as via a newInstance()-style factory method.

Also how would I be able to do this same thing using android.app.Fragment to create a new instances of Fragments?

You wouldn't, because Fragment itself is fairly useless on its own. You create subclasses of Fragment to implement your pages, the way you do any fragment in Android.

I have a variety of Fragments that I want to pass into an ArrayPagerAdapter instead of just one like in the example

You would need to know, based on the PageDescriptor, how to create the right fragment instance. That might be via an int or something in your custom PageDescriptor implementation, that you then use with a switch in createFragment().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for answering that cleared up a lot. Only thing that is still a little cloudy for me now is what type to return in createFragment. In my code I have tried returning a Fragment and android.app.Fragment from createFragment and each gives me an error when trying to do so. The variety of Fragments I am trying to create using createFragment work perfectly fine when I call newInstance on them and return them in the getItem method of the FragmentPagerAdapter I currently have set up. Is this similar to how createFragment is supposed to work? – Blaine Oct 08 '15 at 02:28
  • @Blaine: "each gives me an error when trying to do so" -- I can't really help you without knowing the error. Given your propensity for typing in the fully-qualified `android.app.Fragment`, my guess that [you are using the wrong `ArrayPagerAdapter` class](https://github.com/commonsguy/cwac-pager#choosing-the-package), but that is just a guess. – CommonsWare Oct 08 '15 at 11:11
  • I have continued this in another question I asked at this link http://stackoverflow.com/questions/33093955/dynamically-adding-and-removing-tabs-using-arraypageradapter. Thanks for all the help so far. – Blaine Oct 13 '15 at 04:11