How to delete a single View inside a ScrollView









up vote
1
down vote

favorite












I have a ScrollView inside my activity, to which Views are added during runtime. Basically, when the player defines his name and gender (and names and genders of other players) and then clicks the plus button, then three elements are added to a single line inside the ScrollView: one ImageView for the gender (which contains either male or female icon), a TextView to display the player's name and a delete button at the end of the line, so this player can be deleted, for example if the user mistyped the player's name.



If n is the number of players that are added (or the number the plus button has been clicked), then the number of Views inside the ScrollView is n*3.



After the plus button to add the player is clicked, I also create a PlayerDraft instance, which contains the name of the player, the player's gender and three integers corresponding to views that are associated with the player (one int for the id of the corresponding icon, one int for the id of the corresponding text view, which represents the player's name and one int for the id of the delete button associated with the player).



I also keep track between the ids of the delete buttons and this PlayerDraft instances in a hashmap. This way, when a certain delete button is clicked, I can use it's id to find the corresponding PlayerDraft instance and therefore also the ids of the Views associated with this player inside the ScrollView.



This is the code that is called each time a certain delete button is clicked (this is basically inside a method which I call on every delete button that is added to set it's on click listener instance ... this code is inside the setOnClickListener method):



public void onClick(View v)
//delete corresponding player gender icon, player name and the delete button itself
PlayerDraft playerToDelete = idToPlayer.get(v.getId());
if(null != playerToDelete)
ImageView iconToDelete = findViewById(playerToDelete.getIconViewId());
TextView textToDelete = findViewById(playerToDelete.getNameViewId());
Button b = (Button) v;

Log.d("thc", "Icon id that is to be deleted: " + iconToDelete.getId());
Log.d("thc", "name view id that is to be deleted: " + textToDelete.getId());
Log.d("thc", "delete button id to be deleted: " + b.getId());

player_display_view.removeView(iconToDelete);
player_display_view.removeViewInLayout(textToDelete);
player_display_view.removeViewInLayout(b);

//player_display_view.removeViewAt(0);
//player_display_view.removeAllViews()

Log.d("thc", "Number of elements in player_display_view scrollview: " + player_display_view.getChildCount());
else
Log.d("thc", "playerToDelete is null!");

}


The problem is that calling player_display_view.removeView(iconToDelete) or any similar thereof method doesn't work! However, calling player_display_view.removeAllViews() works.



I've discovered the following problem. The player_display_view ScrollView always contains only one child and that child is at position 0 in it's array of children. The number of children doesn't change no matter how much views are added to the ScrollView during runtime, which just boggles my ming :/. How can the Views even be displayed inside the ScrollView (they are, testing it on my phone), if they aren't specified as children of that ScrollView? Calling the method player_display_view.removeViewAt(0); to delete this only child view inside the ScrollView, deletes not only one View of the ScrollView, but ALL of the added views.



Why is the following happening? Why does the ScrollView contain only one child, despite multiple Views are added to it during runtime? How can I delete specific Views inside the ScrollView?



Thank you for the time taken, I really appreciate it










share|improve this question

























    up vote
    1
    down vote

    favorite












    I have a ScrollView inside my activity, to which Views are added during runtime. Basically, when the player defines his name and gender (and names and genders of other players) and then clicks the plus button, then three elements are added to a single line inside the ScrollView: one ImageView for the gender (which contains either male or female icon), a TextView to display the player's name and a delete button at the end of the line, so this player can be deleted, for example if the user mistyped the player's name.



    If n is the number of players that are added (or the number the plus button has been clicked), then the number of Views inside the ScrollView is n*3.



    After the plus button to add the player is clicked, I also create a PlayerDraft instance, which contains the name of the player, the player's gender and three integers corresponding to views that are associated with the player (one int for the id of the corresponding icon, one int for the id of the corresponding text view, which represents the player's name and one int for the id of the delete button associated with the player).



    I also keep track between the ids of the delete buttons and this PlayerDraft instances in a hashmap. This way, when a certain delete button is clicked, I can use it's id to find the corresponding PlayerDraft instance and therefore also the ids of the Views associated with this player inside the ScrollView.



    This is the code that is called each time a certain delete button is clicked (this is basically inside a method which I call on every delete button that is added to set it's on click listener instance ... this code is inside the setOnClickListener method):



    public void onClick(View v)
    //delete corresponding player gender icon, player name and the delete button itself
    PlayerDraft playerToDelete = idToPlayer.get(v.getId());
    if(null != playerToDelete)
    ImageView iconToDelete = findViewById(playerToDelete.getIconViewId());
    TextView textToDelete = findViewById(playerToDelete.getNameViewId());
    Button b = (Button) v;

    Log.d("thc", "Icon id that is to be deleted: " + iconToDelete.getId());
    Log.d("thc", "name view id that is to be deleted: " + textToDelete.getId());
    Log.d("thc", "delete button id to be deleted: " + b.getId());

    player_display_view.removeView(iconToDelete);
    player_display_view.removeViewInLayout(textToDelete);
    player_display_view.removeViewInLayout(b);

    //player_display_view.removeViewAt(0);
    //player_display_view.removeAllViews()

    Log.d("thc", "Number of elements in player_display_view scrollview: " + player_display_view.getChildCount());
    else
    Log.d("thc", "playerToDelete is null!");

    }


    The problem is that calling player_display_view.removeView(iconToDelete) or any similar thereof method doesn't work! However, calling player_display_view.removeAllViews() works.



    I've discovered the following problem. The player_display_view ScrollView always contains only one child and that child is at position 0 in it's array of children. The number of children doesn't change no matter how much views are added to the ScrollView during runtime, which just boggles my ming :/. How can the Views even be displayed inside the ScrollView (they are, testing it on my phone), if they aren't specified as children of that ScrollView? Calling the method player_display_view.removeViewAt(0); to delete this only child view inside the ScrollView, deletes not only one View of the ScrollView, but ALL of the added views.



    Why is the following happening? Why does the ScrollView contain only one child, despite multiple Views are added to it during runtime? How can I delete specific Views inside the ScrollView?



    Thank you for the time taken, I really appreciate it










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a ScrollView inside my activity, to which Views are added during runtime. Basically, when the player defines his name and gender (and names and genders of other players) and then clicks the plus button, then three elements are added to a single line inside the ScrollView: one ImageView for the gender (which contains either male or female icon), a TextView to display the player's name and a delete button at the end of the line, so this player can be deleted, for example if the user mistyped the player's name.



      If n is the number of players that are added (or the number the plus button has been clicked), then the number of Views inside the ScrollView is n*3.



      After the plus button to add the player is clicked, I also create a PlayerDraft instance, which contains the name of the player, the player's gender and three integers corresponding to views that are associated with the player (one int for the id of the corresponding icon, one int for the id of the corresponding text view, which represents the player's name and one int for the id of the delete button associated with the player).



      I also keep track between the ids of the delete buttons and this PlayerDraft instances in a hashmap. This way, when a certain delete button is clicked, I can use it's id to find the corresponding PlayerDraft instance and therefore also the ids of the Views associated with this player inside the ScrollView.



      This is the code that is called each time a certain delete button is clicked (this is basically inside a method which I call on every delete button that is added to set it's on click listener instance ... this code is inside the setOnClickListener method):



      public void onClick(View v)
      //delete corresponding player gender icon, player name and the delete button itself
      PlayerDraft playerToDelete = idToPlayer.get(v.getId());
      if(null != playerToDelete)
      ImageView iconToDelete = findViewById(playerToDelete.getIconViewId());
      TextView textToDelete = findViewById(playerToDelete.getNameViewId());
      Button b = (Button) v;

      Log.d("thc", "Icon id that is to be deleted: " + iconToDelete.getId());
      Log.d("thc", "name view id that is to be deleted: " + textToDelete.getId());
      Log.d("thc", "delete button id to be deleted: " + b.getId());

      player_display_view.removeView(iconToDelete);
      player_display_view.removeViewInLayout(textToDelete);
      player_display_view.removeViewInLayout(b);

      //player_display_view.removeViewAt(0);
      //player_display_view.removeAllViews()

      Log.d("thc", "Number of elements in player_display_view scrollview: " + player_display_view.getChildCount());
      else
      Log.d("thc", "playerToDelete is null!");

      }


      The problem is that calling player_display_view.removeView(iconToDelete) or any similar thereof method doesn't work! However, calling player_display_view.removeAllViews() works.



      I've discovered the following problem. The player_display_view ScrollView always contains only one child and that child is at position 0 in it's array of children. The number of children doesn't change no matter how much views are added to the ScrollView during runtime, which just boggles my ming :/. How can the Views even be displayed inside the ScrollView (they are, testing it on my phone), if they aren't specified as children of that ScrollView? Calling the method player_display_view.removeViewAt(0); to delete this only child view inside the ScrollView, deletes not only one View of the ScrollView, but ALL of the added views.



      Why is the following happening? Why does the ScrollView contain only one child, despite multiple Views are added to it during runtime? How can I delete specific Views inside the ScrollView?



      Thank you for the time taken, I really appreciate it










      share|improve this question













      I have a ScrollView inside my activity, to which Views are added during runtime. Basically, when the player defines his name and gender (and names and genders of other players) and then clicks the plus button, then three elements are added to a single line inside the ScrollView: one ImageView for the gender (which contains either male or female icon), a TextView to display the player's name and a delete button at the end of the line, so this player can be deleted, for example if the user mistyped the player's name.



      If n is the number of players that are added (or the number the plus button has been clicked), then the number of Views inside the ScrollView is n*3.



      After the plus button to add the player is clicked, I also create a PlayerDraft instance, which contains the name of the player, the player's gender and three integers corresponding to views that are associated with the player (one int for the id of the corresponding icon, one int for the id of the corresponding text view, which represents the player's name and one int for the id of the delete button associated with the player).



      I also keep track between the ids of the delete buttons and this PlayerDraft instances in a hashmap. This way, when a certain delete button is clicked, I can use it's id to find the corresponding PlayerDraft instance and therefore also the ids of the Views associated with this player inside the ScrollView.



      This is the code that is called each time a certain delete button is clicked (this is basically inside a method which I call on every delete button that is added to set it's on click listener instance ... this code is inside the setOnClickListener method):



      public void onClick(View v)
      //delete corresponding player gender icon, player name and the delete button itself
      PlayerDraft playerToDelete = idToPlayer.get(v.getId());
      if(null != playerToDelete)
      ImageView iconToDelete = findViewById(playerToDelete.getIconViewId());
      TextView textToDelete = findViewById(playerToDelete.getNameViewId());
      Button b = (Button) v;

      Log.d("thc", "Icon id that is to be deleted: " + iconToDelete.getId());
      Log.d("thc", "name view id that is to be deleted: " + textToDelete.getId());
      Log.d("thc", "delete button id to be deleted: " + b.getId());

      player_display_view.removeView(iconToDelete);
      player_display_view.removeViewInLayout(textToDelete);
      player_display_view.removeViewInLayout(b);

      //player_display_view.removeViewAt(0);
      //player_display_view.removeAllViews()

      Log.d("thc", "Number of elements in player_display_view scrollview: " + player_display_view.getChildCount());
      else
      Log.d("thc", "playerToDelete is null!");

      }


      The problem is that calling player_display_view.removeView(iconToDelete) or any similar thereof method doesn't work! However, calling player_display_view.removeAllViews() works.



      I've discovered the following problem. The player_display_view ScrollView always contains only one child and that child is at position 0 in it's array of children. The number of children doesn't change no matter how much views are added to the ScrollView during runtime, which just boggles my ming :/. How can the Views even be displayed inside the ScrollView (they are, testing it on my phone), if they aren't specified as children of that ScrollView? Calling the method player_display_view.removeViewAt(0); to delete this only child view inside the ScrollView, deletes not only one View of the ScrollView, but ALL of the added views.



      Why is the following happening? Why does the ScrollView contain only one child, despite multiple Views are added to it during runtime? How can I delete specific Views inside the ScrollView?



      Thank you for the time taken, I really appreciate it







      android android-view android-scrollview android-viewgroup






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 21:47









      AnimaVivens

      324




      324






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          In general, a ScrollView contains only one child. This is usually a container (like LinearLayout or RelativeLayout) that is scrolled by the ScrollView. If you are dynamically (programatically) adding Views to a ScrollView, it has probably created an internal container Layout to hold your added views.



          You should put a Layout inside your ScrollView and manage (add/delete) your Views inside that Layout. Then your "delete" code should work.






          share|improve this answer
















          • 1




            Thank you, your solution has worked! I've introduced another variable, which is of type LinearLayout and called it player_display_layout and got it by accessing the only child of the ScrollView (which is this default-created linear layout). Then, calling removeView on this linear layout (player_display_layout) produces the desired results. Thank you!
            – AnimaVivens
            Nov 10 at 22:33










          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%2f53243733%2fhow-to-delete-a-single-view-inside-a-scrollview%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          In general, a ScrollView contains only one child. This is usually a container (like LinearLayout or RelativeLayout) that is scrolled by the ScrollView. If you are dynamically (programatically) adding Views to a ScrollView, it has probably created an internal container Layout to hold your added views.



          You should put a Layout inside your ScrollView and manage (add/delete) your Views inside that Layout. Then your "delete" code should work.






          share|improve this answer
















          • 1




            Thank you, your solution has worked! I've introduced another variable, which is of type LinearLayout and called it player_display_layout and got it by accessing the only child of the ScrollView (which is this default-created linear layout). Then, calling removeView on this linear layout (player_display_layout) produces the desired results. Thank you!
            – AnimaVivens
            Nov 10 at 22:33














          up vote
          2
          down vote



          accepted










          In general, a ScrollView contains only one child. This is usually a container (like LinearLayout or RelativeLayout) that is scrolled by the ScrollView. If you are dynamically (programatically) adding Views to a ScrollView, it has probably created an internal container Layout to hold your added views.



          You should put a Layout inside your ScrollView and manage (add/delete) your Views inside that Layout. Then your "delete" code should work.






          share|improve this answer
















          • 1




            Thank you, your solution has worked! I've introduced another variable, which is of type LinearLayout and called it player_display_layout and got it by accessing the only child of the ScrollView (which is this default-created linear layout). Then, calling removeView on this linear layout (player_display_layout) produces the desired results. Thank you!
            – AnimaVivens
            Nov 10 at 22:33












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          In general, a ScrollView contains only one child. This is usually a container (like LinearLayout or RelativeLayout) that is scrolled by the ScrollView. If you are dynamically (programatically) adding Views to a ScrollView, it has probably created an internal container Layout to hold your added views.



          You should put a Layout inside your ScrollView and manage (add/delete) your Views inside that Layout. Then your "delete" code should work.






          share|improve this answer












          In general, a ScrollView contains only one child. This is usually a container (like LinearLayout or RelativeLayout) that is scrolled by the ScrollView. If you are dynamically (programatically) adding Views to a ScrollView, it has probably created an internal container Layout to hold your added views.



          You should put a Layout inside your ScrollView and manage (add/delete) your Views inside that Layout. Then your "delete" code should work.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 22:05









          David Wasser

          67.8k10136199




          67.8k10136199







          • 1




            Thank you, your solution has worked! I've introduced another variable, which is of type LinearLayout and called it player_display_layout and got it by accessing the only child of the ScrollView (which is this default-created linear layout). Then, calling removeView on this linear layout (player_display_layout) produces the desired results. Thank you!
            – AnimaVivens
            Nov 10 at 22:33












          • 1




            Thank you, your solution has worked! I've introduced another variable, which is of type LinearLayout and called it player_display_layout and got it by accessing the only child of the ScrollView (which is this default-created linear layout). Then, calling removeView on this linear layout (player_display_layout) produces the desired results. Thank you!
            – AnimaVivens
            Nov 10 at 22:33







          1




          1




          Thank you, your solution has worked! I've introduced another variable, which is of type LinearLayout and called it player_display_layout and got it by accessing the only child of the ScrollView (which is this default-created linear layout). Then, calling removeView on this linear layout (player_display_layout) produces the desired results. Thank you!
          – AnimaVivens
          Nov 10 at 22:33




          Thank you, your solution has worked! I've introduced another variable, which is of type LinearLayout and called it player_display_layout and got it by accessing the only child of the ScrollView (which is this default-created linear layout). Then, calling removeView on this linear layout (player_display_layout) produces the desired results. Thank you!
          – AnimaVivens
          Nov 10 at 22:33

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243733%2fhow-to-delete-a-single-view-inside-a-scrollview%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

          Kleinkühnau

          Makov (Slowakei)

          Deutsches Schauspielhaus