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 7587933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:47:52+00:00 2026-05-30T19:47:52+00:00

I have a very peculiar problem. I understand that while in the foreground the

  • 0

I have a very peculiar problem. I understand that while in the foreground the member variables of an Activity remain untouched (don’t need saving to persist). Now, I have a PagerAdapter that represents a quiz with a question and three answers. I’ve made various objects to represent the question and answers.

A Question always has a list of exactly 3 Answer‘s. This is how I do it: (note: some code was left out for readability)

public class QuizPagerAdapter
{
    List<Question> questionList;
    Context context;

    public QuizPagerAdapter (List<Question> list, Context ctx) {
        questionList = list;
        context = ctx;
    }

    @Override
    public Object instantiateItem (View pager, int position) {
        // Create various views (ScrollView, LinearLayout, etc)

        final Question q = questionList.get(position);

        RadioGroup group = new RadioGroup(context);
        for (int i = 0; i < 3; i++) {
            Answer a = q.getAnswer(i);

            RadioButton rb = new RadioButton(context);
            rb.setId(i);
            rb.setText(a.getText());

            rb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    RadioButton btn = (RadioButton) v;
                    q.unselectAll(); // unselects all Answer's


                    q.getAnswer(btn.getId()).setSelected(true);
                }
            });

            if (a.isSelected()) rb.setChecked(true);
        }
        return view;
    }
}

When I use this in my Activity, I get a nice list of Questions with three Answers. Now, when I select one Answer, the RadioButton is checked. OK, now, I scroll to the next Question and check that one, then go back to the previous Question and there the answer I checked in the first place is now not checked.

Now, this doesn’t always happen, it happens sometimes, but that sometimes is enough that it can cause a problem. The Activity is never placed in the background.

I understand that Android will destroy Views in the PagerAdapter to conserve memory, thus destroying the RadioButton, this is why I have this snippet:

if (a.isSelected()) rb.setChecked(true);

But it is still happening. I’ve tried logging everything, debugging but I cant seem to get to the source of the problem. Maybe someone here has come across a similar problem.

Any help is appreciated,
Thank you in advance!

  • 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-05-30T19:47:53+00:00Added an answer on May 30, 2026 at 7:47 pm

    In the OnClick event, make sure that your reference to Question is the correct object. You might want to reference the question by the list index, because if you have two Questions on the screen at the same time, the reference of q could be the second question, but you have clicked the first questions answer.

    ** Added **
    The issue is that q is a reference to a question, and each time through the instantiateItem function, this reference is set to a new instance of a question from the list. So when you execute the OnClick event, the value q might be pointing to the incorrect instance of question than what you expect. You would be better off getting the question out of the original list by the views index in the activity.

    Something like the following:

    public void onClick(View v) {
       RadioButton btn = (RadioButton) v;
       Question question = questionList.get(position);
       question.unselectAll(); // unselects all Answer's  
       question.getAnswer(btn.getId()).setSelected(true);
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have very simple problem. I need to create model, that represent element of
I have very general problem on DNN 6.0 web site that I am working
I have a very peculiar problem. In the VS2010 Server Explorer, I can connect
I am dealing with a very peculiar problem at the moment: I have an
I have a very peculiar problem:(not using web.config) DefaultFacebookApplication dfa=new DefaultFacebookApplication(); dfa.AppId = ...;
I have a very peculiar problem across the IE board (ignoring IE6). Basically I
A very peculiar bug: I have BarButtonItem which pops out a UIPopoverController like that:
I have very simple query. I want to make sure that I don't have
I have a little peculiar problem that I currently solve using a table ,
I have a very peculiar problem and my last resort was asking this on

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.