Add next row dynamically after third column









up vote
0
down vote

favorite












this is desired table layout:



+++++++++++++
+ + + +
+ 1 + 2 + 3 +
+ + + +
+++++++++++++
+ + + +
+ 4 + 5 + 6 +
+ + + +
+++++++++++++
+ + + +
+ 7 + 8 + - +
+ + + +
+++++++++++++


My table is dynamically created based on json data:



TableLayout tl = (TableLayout) findViewById(R.id.table);
JSONArray jsonArray = new JSONArray( all_gpio_json );

for ( i = 0; i < jsonArray.length(); i++)

...some dynamic table generation code like this:
TableRow row = new TableRow(this);
Switch sw = new Switch( this );
row.addView(sw);


tl.addView( row );


There is no problem to have table generated so, that each json value is represented by unique row, however i'd like to have layout that will have maximum 3 columns per row, and number of rows will be dynamically growing as long as there are json data in the loop.



I tried to add new row after existing row has 3 items filled already:



if (i % 3 == 1) TableRow new_row = new TableRow( this );


But im getting an error when new_row is supposed to be added:



 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.comain.tests, PID: 15519
java.lang.RuntimeException: Unable to start activity ComponentInfocom.comainxxx.tests/com.comainxxx.tests.MainActivity: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.


Anyone would have an idea how to achieve this to add new row on the fly?



EDIT: Solved by implementing grid view, that is more suitable for this task.










share|improve this question























  • what is mean exactly about this sentence of u ??? (But this is not possible in java i guess)
    – hamid keyhani
    Nov 10 at 6:42










  • i updated my question adding the error code
    – B.Ondrej
    Nov 10 at 6:58














up vote
0
down vote

favorite












this is desired table layout:



+++++++++++++
+ + + +
+ 1 + 2 + 3 +
+ + + +
+++++++++++++
+ + + +
+ 4 + 5 + 6 +
+ + + +
+++++++++++++
+ + + +
+ 7 + 8 + - +
+ + + +
+++++++++++++


My table is dynamically created based on json data:



TableLayout tl = (TableLayout) findViewById(R.id.table);
JSONArray jsonArray = new JSONArray( all_gpio_json );

for ( i = 0; i < jsonArray.length(); i++)

...some dynamic table generation code like this:
TableRow row = new TableRow(this);
Switch sw = new Switch( this );
row.addView(sw);


tl.addView( row );


There is no problem to have table generated so, that each json value is represented by unique row, however i'd like to have layout that will have maximum 3 columns per row, and number of rows will be dynamically growing as long as there are json data in the loop.



I tried to add new row after existing row has 3 items filled already:



if (i % 3 == 1) TableRow new_row = new TableRow( this );


But im getting an error when new_row is supposed to be added:



 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.comain.tests, PID: 15519
java.lang.RuntimeException: Unable to start activity ComponentInfocom.comainxxx.tests/com.comainxxx.tests.MainActivity: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.


Anyone would have an idea how to achieve this to add new row on the fly?



EDIT: Solved by implementing grid view, that is more suitable for this task.










share|improve this question























  • what is mean exactly about this sentence of u ??? (But this is not possible in java i guess)
    – hamid keyhani
    Nov 10 at 6:42










  • i updated my question adding the error code
    – B.Ondrej
    Nov 10 at 6:58












up vote
0
down vote

favorite









up vote
0
down vote

favorite











this is desired table layout:



+++++++++++++
+ + + +
+ 1 + 2 + 3 +
+ + + +
+++++++++++++
+ + + +
+ 4 + 5 + 6 +
+ + + +
+++++++++++++
+ + + +
+ 7 + 8 + - +
+ + + +
+++++++++++++


My table is dynamically created based on json data:



TableLayout tl = (TableLayout) findViewById(R.id.table);
JSONArray jsonArray = new JSONArray( all_gpio_json );

for ( i = 0; i < jsonArray.length(); i++)

...some dynamic table generation code like this:
TableRow row = new TableRow(this);
Switch sw = new Switch( this );
row.addView(sw);


tl.addView( row );


There is no problem to have table generated so, that each json value is represented by unique row, however i'd like to have layout that will have maximum 3 columns per row, and number of rows will be dynamically growing as long as there are json data in the loop.



I tried to add new row after existing row has 3 items filled already:



if (i % 3 == 1) TableRow new_row = new TableRow( this );


But im getting an error when new_row is supposed to be added:



 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.comain.tests, PID: 15519
java.lang.RuntimeException: Unable to start activity ComponentInfocom.comainxxx.tests/com.comainxxx.tests.MainActivity: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.


Anyone would have an idea how to achieve this to add new row on the fly?



EDIT: Solved by implementing grid view, that is more suitable for this task.










share|improve this question















this is desired table layout:



+++++++++++++
+ + + +
+ 1 + 2 + 3 +
+ + + +
+++++++++++++
+ + + +
+ 4 + 5 + 6 +
+ + + +
+++++++++++++
+ + + +
+ 7 + 8 + - +
+ + + +
+++++++++++++


My table is dynamically created based on json data:



TableLayout tl = (TableLayout) findViewById(R.id.table);
JSONArray jsonArray = new JSONArray( all_gpio_json );

for ( i = 0; i < jsonArray.length(); i++)

...some dynamic table generation code like this:
TableRow row = new TableRow(this);
Switch sw = new Switch( this );
row.addView(sw);


tl.addView( row );


There is no problem to have table generated so, that each json value is represented by unique row, however i'd like to have layout that will have maximum 3 columns per row, and number of rows will be dynamically growing as long as there are json data in the loop.



I tried to add new row after existing row has 3 items filled already:



if (i % 3 == 1) TableRow new_row = new TableRow( this );


But im getting an error when new_row is supposed to be added:



 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.comain.tests, PID: 15519
java.lang.RuntimeException: Unable to start activity ComponentInfocom.comainxxx.tests/com.comainxxx.tests.MainActivity: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.


Anyone would have an idea how to achieve this to add new row on the fly?



EDIT: Solved by implementing grid view, that is more suitable for this task.







android






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 7:40

























asked Nov 9 at 21:58









B.Ondrej

589




589











  • what is mean exactly about this sentence of u ??? (But this is not possible in java i guess)
    – hamid keyhani
    Nov 10 at 6:42










  • i updated my question adding the error code
    – B.Ondrej
    Nov 10 at 6:58
















  • what is mean exactly about this sentence of u ??? (But this is not possible in java i guess)
    – hamid keyhani
    Nov 10 at 6:42










  • i updated my question adding the error code
    – B.Ondrej
    Nov 10 at 6:58















what is mean exactly about this sentence of u ??? (But this is not possible in java i guess)
– hamid keyhani
Nov 10 at 6:42




what is mean exactly about this sentence of u ??? (But this is not possible in java i guess)
– hamid keyhani
Nov 10 at 6:42












i updated my question adding the error code
– B.Ondrej
Nov 10 at 6:58




i updated my question adding the error code
– B.Ondrej
Nov 10 at 6:58












2 Answers
2






active

oldest

votes

















up vote
0
down vote













It is possible in java. U need to declare that variable outside the if condition. Try below code



 TableRow new_row = new TableRow(this);

for ( i = 0; i < jsonArray.length(); i++)

if (i % 3 == 0 && i != 0) new_row = new TableRow( this );
.......
Switch sw = new Switch( this );
new_row .addView(sw);







share|improve this answer



























    up vote
    0
    down vote













    EDIT: Solved by implementing grid view, that is more suitable for this task.






    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233767%2fadd-next-row-dynamically-after-third-column%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      It is possible in java. U need to declare that variable outside the if condition. Try below code



       TableRow new_row = new TableRow(this);

      for ( i = 0; i < jsonArray.length(); i++)

      if (i % 3 == 0 && i != 0) new_row = new TableRow( this );
      .......
      Switch sw = new Switch( this );
      new_row .addView(sw);







      share|improve this answer
























        up vote
        0
        down vote













        It is possible in java. U need to declare that variable outside the if condition. Try below code



         TableRow new_row = new TableRow(this);

        for ( i = 0; i < jsonArray.length(); i++)

        if (i % 3 == 0 && i != 0) new_row = new TableRow( this );
        .......
        Switch sw = new Switch( this );
        new_row .addView(sw);







        share|improve this answer






















          up vote
          0
          down vote










          up vote
          0
          down vote









          It is possible in java. U need to declare that variable outside the if condition. Try below code



           TableRow new_row = new TableRow(this);

          for ( i = 0; i < jsonArray.length(); i++)

          if (i % 3 == 0 && i != 0) new_row = new TableRow( this );
          .......
          Switch sw = new Switch( this );
          new_row .addView(sw);







          share|improve this answer












          It is possible in java. U need to declare that variable outside the if condition. Try below code



           TableRow new_row = new TableRow(this);

          for ( i = 0; i < jsonArray.length(); i++)

          if (i % 3 == 0 && i != 0) new_row = new TableRow( this );
          .......
          Switch sw = new Switch( this );
          new_row .addView(sw);








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 5:43









          Lucefer

          9961511




          9961511






















              up vote
              0
              down vote













              EDIT: Solved by implementing grid view, that is more suitable for this task.






              share|improve this answer
























                up vote
                0
                down vote













                EDIT: Solved by implementing grid view, that is more suitable for this task.






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  EDIT: Solved by implementing grid view, that is more suitable for this task.






                  share|improve this answer












                  EDIT: Solved by implementing grid view, that is more suitable for this task.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 at 7:41









                  B.Ondrej

                  589




                  589



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233767%2fadd-next-row-dynamically-after-third-column%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo