Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9139579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:24:22+00:00 2026-06-17T09:24:22+00:00

i’m pretty new with Fragments and ViewPager. I’m using ActionBarSherlock and ViewPageIndicator from Jack

  • 0

i’m pretty new with Fragments and ViewPager. I’m using ActionBarSherlock and ViewPageIndicator from Jack Wharton.

I’ve started with a standard Android MasterDetailFlow Activity and did try to modify it to use a ViewPager in the detail part.

I’m using the standard DummyContent to provide some static data but i’ve replaced the DummyItem with my “Survey”-Library i have to use in this app. DummyContent provides a public static ArrayList which i use to fill the list in the list activity. After i choose a survey in this list, the corresponding questions should be shown in the view pager.

Here is the code of my QuestionActivity.java which hosts the question fragments.

    public class QuestionActivity extends SherlockFragmentActivity {

    private QuestionsFragmentPagerAdapter mAdapter;
    private PageIndicator mIndicator;
    private ViewPager mPager;

    private String surveyName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_viewpager);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        surveyName = getIntent().getExtras().getString(ItemDetailFragment.ARG_SURVEY_NAME);

        mAdapter = new QuestionsFragmentPagerAdapter(getSupportFragmentManager(), DummyContent.mgr.getSurvey(surveyName).getQuestions());

        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        mIndicator = (PageIndicator) findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);
    }
}

QuestionsFragmentPagerAdapter.java

public class QuestionsFragmentPagerAdapter extends FragmentPagerAdapter {

    ArrayList<Question> questions;

    public QuestionsFragmentPagerAdapter(FragmentManager fm, List<Question> questions) {
        super(fm);

        this.questions = (ArrayList<Question>) questions;
    }

    @Override
    public Fragment getItem(int position) {

        Fragment f = QuestionFragment.newInstance(questions.get(position));
        return  f;
    }

    @Override
    public int getCount() {
        return questions.size();
    }
}

QuestionFragment.java

public class QuestionFragment extends SherlockListFragment {

    protected enum QuestionType {
        FT, SC, MC;
    }

    public final static String ARG_QUESTION_QUESTION = "question_question";
    public final static String ARG_QUESTION_TYPE = "question_type";
    public final static String ARG_QUESTION_ANSWERINGOPTIONS = "question_answeringptions";

    private TextView lblQuestion;
    private EditText txtAnswer;
    private ListView listAnswers;

    private ArrayAdapter<String> listAdapter;

    private Question question;
    private int listLayout;

    /**
     * 
     * @param question
     * @return
     */
    public static QuestionFragment newInstance(Question question) {

        QuestionFragment fragment = new QuestionFragment();

        // Creates a Bundle with all informations available in the question obj.
        Bundle args = createBundleFromQuestion(question);

        fragment.setArguments(args);

        return fragment;
    }

    /**
     * 
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Creates the question object from the given arguments.
        // I know this isn't a good solution, i will implement the
        // Parcelable asap i have solved the current issues.
        // 
        createQuestionFromBundle(getArguments());

//      String questionXml = getArguments() != null ? getArguments().getString(ARG_QUESTION_XML) : null;
//      this.question = (Question) MyXmlSerializer.deserialize(questionXml, Question.class);
    }

    /**
     * Creates a the Question object form the Bundle.
     * @param extras
     */
    private void createQuestionFromBundle(Bundle extras) {
        // Think we don't need it here. The field question gets instantiated.
    }

    /**
     * 
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        return inflater.inflate(R.layout.answer_question, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initWidgets();
        setCorrectLayout();
        initContent();
    }

    private void initContent() {
        String questionStr = question.getQuestion();
        lblQuestion.setText(questionStr);

        if(question instanceof FTQuestion) {

        } else if (question instanceof ClosedQuestion) {

            listAdapter = new ArrayAdapter<String>(getActivity(), listLayout);

            List<Answer> answeringOptions = question.getAnswers();
            for(Answer answer : answeringOptions) {
                listAdapter.add(answer.getAnswer());
            }

            listAnswers.setAdapter(listAdapter);
        }
    }

    /**
     * 
     */
    private void initWidgets() {
        listAnswers = getListView();
        lblQuestion = (TextView) getActivity().findViewById(R.id.lblQuestion);
        txtAnswer = (EditText) getActivity().findViewById(R.id.txtAnswer);
    }

    /**
     * Sets the FT/SC/MC layout
     */
    private void setCorrectLayout() {
        if(question instanceof FTQuestion) {
            setFtLayout();
        } else if (question instanceof SCQuestion) {
            setScLayout();
        } else if (question instanceof MCQuestion) {
            setMcLayout();
        }
    }

    /**
     * 
     */
    private void setFtLayout() {
        if(listAnswers.getVisibility()!=ListView.INVISIBLE && listAnswers.getVisibility()!=ListView.GONE) {
            listAnswers.setVisibility(ListView.GONE);
        }
    }

    /**
     * 
     */
    private void setScLayout() {
        listLayout = R.layout.answer_question_single_choice_list_row;
        listAnswers.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        if(txtAnswer.getVisibility() == TextView.VISIBLE) txtAnswer.setVisibility(TextView.GONE);
    }

    /**
     * 
     */
    private void setMcLayout() {
        listLayout = R.layout.answer_question_multiple_choice_list_row;
        listAnswers.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        if(txtAnswer.getVisibility() == TextView.VISIBLE) txtAnswer.setVisibility(TextView.GONE);
    }
}

Choosing the right survey in the list works fine, but now the questions are displayed totaly wrong.

Actually there should be now 3 pages with 3 different questions. On the first page there should be a label with a question”Eine tolle FT Frage?” and below this label an EditText. On the second page there should be a label with a question “Eine tolle SC Frage?” and below a list with the answering options. On page three the should have the question “Eine tolle MC Frage?” and also a list below it with the same answering options as on page two.

The screenshos show a transition between the pages in the order: 1 -> 2 -> 3 -> 2 -> 1 -> 2.

you can see, that it does not appear in a way i described it above. the content of the pages does also change during the transition. i believe that there could be a problem with the DummyContent because it’s static?!

first page
second page
third page
back to second page, now a question text appears
back to first page, now there is no question text any more
back to second page, again another question text appears

If i create a survey with just one question, everything works fine…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:24:22+00:00Added an answer on June 17, 2026 at 9:24 am

    Okay i’ve found the answer:

    i wanted to initialize the used widgets in the onCreateView Callback. But then i always got “java.lang.IllegalStateException: Content view not yet created”. A closer look showed, that this was just because of the getListView() method.

    Now i switched the initialization of the widgets to the onCreateView() Callback but the getListView() i left in onActivityCreated().

    Now everything works fine, and the fragments are displayed correctly!
    That’s how it looks right now:

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
    
            View v = inflater.inflate(R.layout.answer_question, null);
    
            lblQuestion = (TextView) v.findViewById(R.id.lblQuestion);
            txtAnswer = (EditText) v.findViewById(R.id.txtAnswer);
    
            return v;
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
    
            listAnswers = getListView();
            setCorrectLayout();
            initContent();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I am using jsonparser to parse data and images obtained from json response. When
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am using JSon response to parse title,date content and thumbnail images and place
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.