0

I'm new to Java/Android development. I am building a question/answers dynamically inside a viewflipper so that each flip has a new question with some answers. Right now, in my XML file I have a flipperview. The code below builds X number of [linearlayout with a [radiogroup and [4 radio elements]]]. My question is: how do I get the selected radio button based on the "current" visible window in the flipper?

for (DataQuizQuiz quiz_question : PLT.dataQuiz.getQuiz_data()) {
    LinearLayout ll = new LinearLayout(this);
    ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                        LayoutParams.FILL_PARENT));
    ll.setOrientation(LinearLayout.VERTICAL);
    RadioGroup rg = new RadioGroup(this);

    TextView tv_answer = new TextView(this);
    tv_answer.setText("Question: " + quiz_question.getQuestion());
    ll.addView(tv_answer);

    for (DataQuizAnswers answer : quiz_question.getAnswers()) {
        RadioButton rb = new RadioButton(this);
        rb.setText(answer.getAnswer());
        rg.addView(rb);
    }

    ll.addView(rg);
    vf_quiz_data.addView(ll);
}

All I know is vf_quiz_data.getCurrentView(), but beyond that I do not know how to reference the elements within that, since they do not have an id and are created on-the-fly. The code works to build the layout; I just am not sure how to reference the data inside it now. Thanks for any help.

Update: I came up with a way to target the radio group in the visible view but I think there has to be a better way. I assigned the radio group an id of a counter 0,1,2 etc. as it loops and capture the radio group element using this:

int selected = (int) ((RadioGroup)
vf_quiz_data.getCurrentView().findViewById(
    vf_quiz_data.getDisplayedChild())).getCheckedRadioButtonId();
RadioButton b = (RadioButton) findViewById(selected);
Log.v("DEBUG",(String) b.getText());

I am also not sure how safe assigning ids based off of a counter is. If anyone has a alternate way of doing this please let me know.

Sean Mickey
  • 7,618
  • 2
  • 32
  • 58
Danny
  • 1,185
  • 2
  • 12
  • 33

1 Answers1

0

A friend told me to build a custom view or views for my data so I could access it from my own methods using vf_quiz_data.getCurrentView(). After a bunch of trial and error I got a test example working. My custom class was called "ViewQuiz" and I added a method called "getName()" that returned the value of a edittext that the class added into the view. I ended up being able to retrieve it like this: (ViewQuiz) vf_quiz_data.getCurrentView()).getName(). My class extended linearlayout and out a edittext inside it, I created a new instance of the class in a loop and added it with addView onto a viewflipper. Incase this is helpful for someone else here is the example:

public class ViewQuiz extends LinearLayout {

public EditText name;

public ViewQuiz(Context context, AttributeSet attrs) {
    super(context, attrs);

    name = new EditText(context);

    name.setText("This is a test");
    addView(name);
}

public ViewQuiz(Context context) {
    super(context);
    name = new EditText(context);
    name.setText("This is a test");
    addView(name);
}

public String getName() {
    return name.getText().toString();

}

}

ViewQuiz test;
for (loop stuff here) {
        test = new ViewQuiz(this);
        test.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        // vf_quiz_data = (ViewFlipper) findViewById(R.id.vf_quiz_data);
        vf_quiz_data.addView(test);
}

// get text from visible view
(ViewQuiz) vf_quiz_data.getCurrentView()).getName()
Danny
  • 1,185
  • 2
  • 12
  • 33