简体   繁体   中英

Putting LinearLayout to LinearLayout Array

i wanna create button 1 to 9 and i want to do that in loop. But in each 3 count, i want to create a new LinearLayout.

  final LinearLayout[] ll2 = new LinearLayout[10]; // create an empty array;


            for(int i=1; i<=9;i++)
            {
                Button btnNums = new Button(this);
            final LinearLayout[] ll2 = new LinearLayout[10]; // create an empty array;


            for(int i=1; i<=9;i++)
            {
                Button btnNums = new Button(this);
                btnNums.setText(i+"");
                ll.addView(btnNums);
                if(i%3==0){
                    ll2[i] = ll;
                    ll = null;

                }
            }

            layout.addView(ll2[0]);

    btnNums.setText(i+"");
            ll.addView(btnNums);
            if(i%3==0){
                ll2[i] = ll;
                ll = null;

            }
        }

        layout.addView(ll2[0]);

This does not work. I get no error but when o run the app, it is stopped to work. What's the problem?

I used it in my Project and it worked for me, i used it like this. Hope it helps

Declare empty array at class level:

LinearLayout[] imageLayoutContainers = new LinearLayout[10];

And then in on onCreate methord :

 for (int i = 0; i < imageLayoutContainers.length; i++) {
        imageLayoutContainers[i] = new LinearLayout(this);
        imageLayoutContainers[i].setOrientation(LinearLayout.VERTICAL);
        imageLayoutContainers[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        imageLayoutContainers[i].setBackgroundResource(imagesIds[i]);
    }

It worked, thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM