Adding Items Programmatically

2

I have a certain part of an application in which there is a random questionnaire. Generated by the user. The question is: I do not have access to the amount of questions or what the questions are promptly asked. So I have to get it all dynamically. Therefore, the questionnaire has to be dynamic. And therein lies my doubt. I already got everything. But when it comes to assembling, I'm not getting it.

I use ViewFlipper for the various issues. Within this ViewFlipper are the layouts with the questions. I use ScrollView and within ScrollView a RelativeLayout to assemble the questions. I do not know if this is the right way but it works when I use a fixed questionnaire.

I'm having a layout break when trying to insert the elements dynamically.

RelativeLayout.LayoutParams paramsRelativeLayout = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
paramsRelativeLayout.setMargins(16, 16, 16, 16);

mainLayout = (ViewFlipper) findViewById(R.id.viewFlipper1);

for(int i = 0; i < perguntasLabel.length; i++) {
    viewNotList = new ScrollView(getApplicationContext());
    viewNotList.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    mainLayout.addView(viewNotList);

    viewNotListChild = new RelativeLayout(getApplicationContext());
    viewNotListChild.setLayoutParams(paramsRelativeLayout);
    viewNotListChild.setBackgroundColor(getResources().getColor(R.color.padrao));
    viewNotList.addView(viewNotListChild);

    titulo = new TextView(getApplicationContext());
    titulo.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    titulo.setText(perguntasLabel[i]);
    titulo.setTextSize(20);
    viewNotListChild.addView(titulo);


    if (perguntasTipo[i].equals("datepicker")) {
        EditText input = new EditText(getApplicationContext());
        input.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        input.setBackground(getResources().getDrawable(R.drawable.input));
        viewNotListChild.addView(input);

    }
}
    
asked by anonymous 11.02.2014 / 15:38

1 answer

1

As I said, the problem was solved as follows.

When you created a View , your ID was entered.

view.setId(INTEIRO);

And in% with% of next LayoutParams a rule has been added.

layoutParams.addRule(RelativeLayout.BELOW, viewAnterior.getId());

That's not the right way, most likely. I found a bit of gambiarra but followed the pattern that had already been established in the other forms of the application. Maybe, as suggested by @AnselmoMs, the best option would have been to use View .

    
20.02.2014 / 12:22