0

I'm trying to update/populate xml on run-time. The textViews are displayed fine but it seems like it fails position them correctly after the first item (see the else statement). Is it because getId() is not recognised or am I totally wrong?

for(int x=1; x<13; x++){
    String prompt="PROMPT_"+String.valueOf(x);
    String promptValue = myTacorCursor.getString(myTacorCursor.getColumnIndex(prompt));
    //if not empty draw a row
    if (!promptValue.equals("")){
        //insert new rows into layout
        RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.RelativeLayout1);
        TextView promptLabel = new TextView(this);
        promptLabel.setTextAppearance(this, android.R.style.TextAppearance_DeviceDefault_Large);
        promptLabel.setText(myTacorCursor.getString(myTacorCursor.getColumnIndex("PROMPT_"+String.valueOf(x))));                
        promptLabel.setId(1);
        ((RelativeLayout) myLayout).addView(promptLabel);

        RelativeLayout.LayoutParams mLayoutParams1=(RelativeLayout.LayoutParams)promptLabel.getLayoutParams();
        mLayoutParams1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        if (i==1){
            mLayoutParams1.addRule(RelativeLayout.BELOW,R.id.textView7);
            Log.w("ID is:", String.valueOf(promptLabel.getId()));
        } else{                 
            mLayoutParams1.addRule(RelativeLayout.BELOW,promptLabel.getId());
            Log.w("ID is:", String.valueOf(promptLabel.getId()));
        }
    i++;
    }
}   

I'm trying to display:

(textView)LABEL xx R.id.textview7
<-- here would be the inserted columns -->
(text view) prompt 1 
(text view) prompt 2
(text view) prompt 3
... etc ...'
Cœur
  • 37,241
  • 25
  • 195
  • 267
user2224135
  • 57
  • 1
  • 7

2 Answers2

1

Setting ids dynamically is OK. Just some more attentiveness and your code works.

  1. As far as you're in the for loop, it's better to increment index only in one place.
  2. After you've changed the LayoutParams of the View you need to set it back: promptLabel.setLayoutParams(layoutParams)

Try this. It should work:

public class MainActivity extends Activity {

    private static final int BASE_ID = 1;
    private static final int ITEMS_COUNT = 13;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RelativeLayout rootLayout = (RelativeLayout) findViewById(R.id.root);

        for (int index = BASE_ID; index < ITEMS_COUNT; index++) {
            String prompt = "PROMPT_" + String.valueOf(index);
            // if not empty draw a row
            // insert new rows into layout
            TextView promptLabel = new TextView(this);
            promptLabel.setTextAppearance(this,
                    android.R.style.TextAppearance_DeviceDefault_Large);
            promptLabel.setText(prompt);
            promptLabel.setId(index);
            rootLayout.addView(promptLabel);

            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) promptLabel
                    .getLayoutParams();
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            if (index == BASE_ID) {
                layoutParams.addRule(RelativeLayout.BELOW, R.id.top);
                Log.d("ID is:", String.valueOf(index));
            } else {
                layoutParams.addRule(RelativeLayout.BELOW, index - 1);
                Log.d("ID is:", String.valueOf(index - 1));
            }
            promptLabel.setLayoutParams(layoutParams);
        }
    }
}
riwnodennyk
  • 8,140
  • 4
  • 35
  • 37
0

You don't need to construct the whole layout programatically. Define it in separate layout xml file and then use layout inflater to inflate it. After that add it where you want it.

I have never seen ids assigned programatically, but with my suggestion you can define them in the xml as usual.

PS: Here is a good example explaining how to use LayoutInflater.

Community
  • 1
  • 1
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
  • I can see where you coming from and I've tried to implement it but I have the same problem as before as I want it to be as dynamic as possible. Question: If I'll use LayoutInflater would I be able override the ID of the textView on the fly or will I have to create 13 instances of the textView in the xml as there might be 1, 2 or 13 prompts. – user2224135 Apr 24 '13 at 15:51
  • @user2224135 Why are you aiming at using ids instead of Tags? I know you can programatically control tags. – Boris Strandjev Apr 24 '13 at 16:19
  • It is for better control and data handling as I have many variations of possible layouts. See riwnodennyk answer which solved the problem. Your suggested idea works too though – user2224135 Apr 26 '13 at 08:08